sed and awk

Remove multiple spaces
= = = = = = = = = = = = =

Remove leading space and number in history command output
 
history | sed 's/^ *[0-9][0-9]* *//'


root@vm2:~# cat hello 
#ab 12
cd      99
   zz    te
#ef
    test
      #tellso

root@vm2:~# cat hello  | sed 's/^\s\+//g'

#ab 12
cd      99
zz    te
#ef
test
#tellso

Combinig sed expression
root@vm2:~# cat hello  | sed 's/^\s\+//g;s/\s\+/ /g'
#ab 12
cd 99
zz te
#ef
test
#tellso


sed 'p' passwd  #prints file content but double content
sed -n '1p' file.txt  #print 1st line
sed -n  '2p' file.txt  #print 2nd line
sed -n 4,13p file.txt #print 4 to 14 line
sed -n '$p'  file.txt  #print last
sed -n '1!p' file.txt  #print all except line 1
sed -n '1,3!p' file.txt #print all except line 1 to 3


=======================using regular expression=======================
we use '-e' option when we use expression

sed -n -e '/2011/p' file.txt     # print line containing 2011
sed -n -e '/^root/p' passwd      # print lines starting with word 'root'
sed -n -e '/root$/p' passwd      # print lines Ending with word 'root'
sed -n -e '/^harke101$/p' passwd
sed -n -e '/^harke101.*$/p' passwd     # print lines Starting and Ending with word 'root'
sed -n -e '/[0-9]/p' firewall.txt     # print lines containing number 0-9
sed -n -e '/^[0-9][0-9][0-9]$/p'  firewall.txt #print beginning and ending with 3 char number
sed -n -e '/[0-9]\{3\}/p' firewall.txt # print beginning and ending with 3 char number
sed -n -e '/^[0-9]\{3\}$/p' firewall.txt # print beginning and ending with 3 char number



Find sumtotal of column

ps aux | grep apache | awk '{ sum += $6 } END { print sum }'
ls -lh php*; ls -l php* | awk '{ SUM += $5} END { print SUM/1024/1024 }'
===========================================================================



echo 'its a trap' | sed s/ra/zzz/        ##replace the word ra with zzz, matches only first occurance of Reg Ex
its a tzzzp

echo "how now brown cow" | sed s/ow/YY/  
hYY now brown cow

echo "how now brown cow" | sed s/ow/YY/g
hYY nYY brYYn cYY

sed -ie 's/ow/aagh/g' ~/temp.txt   #Make changes to the file itself

sed '11,$ d' ~/temp.txt  #delete line 11 and latter lines of file

sed '/#.*/ d'  file.txt   #delete line starting with '#'

Remove empty lines and comments {#  and ;}

 cat taste | sed '/^[#;]/d' | sed '/^$/d'      =>[#;] means lines starting with semicolon ";"  or hash " # " 


Tr command in linux
= = = = = = =  = = =  = =

Replace newline[\n]with    '  '  <space>

echo "$string" | tr '\n' ' '

Translate white-space to tabs

$ echo "This is for testing" | tr [:space:] '\t'
This is for testing

We can use -s option to squeeze the repetition of characters.

$ echo "This   is   for testing" | tr -s [:space:] '\t'
This is for testing

# Replace multiple newline with single new line


cat -s filename

cat -s 10-master.conf | grep -v "#" | cat -s

# with sed

cat filename | sed '/^$/d'

# with tr to replace multiple newline to one
cat file name | tr -s '\n'

No comments:

Post a Comment