sed
sed ‘s/https://www.neyash.com/https://www.neyash.com/g’ ./*
sed -n ‘1p;5p’/etc/passwd print 1 and 5line
‘1,5p’ print 1to5 line
‘1p;$p’ print 1st and last line
sed -n ‘1,5p;25p;31,35p’ /etc/passwd
sed -i ‘s/root/network/gi’ /etc/password change file
cat /etc/sample | sed ‘s/root/network/2’ dry run
cat /etc/sample | sed ‘1,15s/root/network/2’ dry run
cat /etc/sample | sed ‘s/root/network/g;s/nologin/cloud/g’ run multiple patern
sed ‘5s/^/#/’ /etc/sample comment 5 no line
sed ‘5,7d’ /etc/sample delete 5 to 7 line no
sed ‘5a Kaustubh’ /etc/sample add kaustubh after line no 5
sed ‘5i Kaustubh’ /etc/sample add kaustubh before line no 5
-i change the file
g globle
i ignore case sensative
2 match patern in no2
1,15s chage patern 1 to 15 line
Grep
grep root /etc/password
-n print line no
-c print count
-o no of time count
-i ignore case sencetive
-R search in dirctory
-v print revert grep patern
-w search fix patern
-A2 print afer 2 line
-B2 print before 2 line
-C3 print common A2 and B2
^root biginig of line
bash$ ending of line
-E search multiple patern in multiple file one command
^[#] search comment line
^[^#] search not comment line
# vi /opt/scripts/disk-usage-multiple.sh
#!/bin/sh
output1=/tmp/disk-usage.out
echo “—————————————————————————”
echo “HostName Filesystem Size Used Avail Use% Mounted on”
echo “—————————————————————————”
for server in `more /opt/scripts/servers.txt`
do
output=`ssh $server ip a | grep global | awk ‘{print $2}’ | awk -F/ ‘{print $1}’`
echo “$server: $output” >> $output1
done
cat $output1 | grep G | column -t
rm $output1
ip=`ip a | grep global | awk ‘{print $2}’ | awk -F/ ‘{print $1}’`
serialno=`cat /sys/devices/virtual/dmi/id/product_serial`
ram=`free -mh | grep Mem | awk ‘{print $2}’`
cpuspeed=`cat /proc/cpuinfo | grep “cpu MHz” | tail -1 | awk -F: ‘{print $2}’`
cpucount=`lscpu | grep CPU | head -2 | tail -1 | awk -F: ‘{print $2}’`
cpucorecount=`lscpu | grep ‘Core(s) per socket’ | awk -F: ‘{print $2}’`
host_name=`hostnamectl | grep ‘hostname’ | tail -1 | awk -F: ‘{print $2}’`
fqdn_host_name=`hostnamectl | grep ‘hostname’ | head -1 | awk -F: ‘{print $2}’`
echo “Server IP $ip Host_Name $host_name FQND_Host $fqdn_host_name Product_Serial $serialno RAM $ram CPU_Speed $cpuspeed CPU_Count $cpucount CPU_Core $cpucorecount
cat server_info.sh
#!/bin/sh
output1=./disk-usage.out
ip=`ip a | grep global | awk ‘{print $2}’ | awk -F/ ‘{print $1}’`
serialno=`cat /sys/devices/virtual/dmi/id/product_serial`
ram=`free -mh | grep Mem | awk ‘{print $2}’`
#cpuspeed=`cat /proc/cpuinfo | grep “cpu MHz” | tail -1 | awk -F: ‘{print $2}’`
cpuspeed=`cat /proc/cpuinfo | grep “cpu MHz” | tail -1 | awk -F: ‘{print $2}’`
cpucount=`lscpu | grep CPU | head -2 | tail -1 | awk -F: ‘{print $2}’`
cpucorecount=`lscpu | grep ‘Core(s) per socket’ | awk -F: ‘{print $2}’`
host_name=`hostnamectl | grep ‘hostname’ | tail -1 | awk -F: ‘{print $2}’`
fqdn_host_name=`hostnamectl | grep ‘hostname’ | head -1 | awk -F: ‘{print $2}’`
echo “Server_IP :” $ip
echo “Server_Host_Name :” $host_name
echo “Server FQND :” $fqdn_host_name
echo “Server SerialNo :” $serialno
echo “Server RAM :” $ram
echo “Server CPUSpeed :” $cpuspeed
echo “Server CPU_Count :” $cpucount
echo “Server CPU_Core_Count :” $cpucorecount
ansible myhost -i mytest -m command -a “sed -e ‘/kaustubh/ s/^#*/#/’ /etc/fstab_org”
Search keyword kaustubh and comment line adhoc command