Here we shall look into a one more very powerful command Awk.
Awk is a field processor unlike grep.
Awk supports egrep compatibale regular expressions like possix.
1.The usage of awk would be like this
awk '/optional_match/ { action }' file_name | Pipe
2.To print all the lines of a file
awk '{ print $1 }' animals.txt
[root@localhost bsurnida]# awk '{print}' animals.txt
i hate cats
ther is a cat
cat is not here
[root@localhost bsurnida]# awk '{print $1}' animals.txt
i
ther
cat
Use single quotes with awk, to avoid shell interpretation of awk's variables
3. awk '{ print $1,$2 }' animals.txt
Input and output field separators is whitespace by default
[root@localhost bsurnida]# awk '{print $1,$2}' animals.txt
i hate
ther is
cat is
4. Search for a word in the lines of a file , same like grep
awk '/cat/ { print } ' animals.txt - this will print a lines containing 'cat'
[root@localhost bsurnida]# awk '/cat/ {print}' animals.txt
i hate cats
ther is a cat
cat is not here
5. Search for a particular work in a particular column
awk '{ if ($2 ~ /cat/) print}' animals.txt
[root@localhost bsurnida]# awk '{if($2 ~/cat/) print}' animals.txt
a cat cant exits
[root@localhost bsurnida]#
Use the i and I options to vary the output
[root@localhost bsurnida]# awk '/cat/ {print}' animals.txt
i hate cats
ther is a cat
cat is not here
a cat cant exits
no he is not taking cat exam
[root@localhost bsurnida]# awk '/cat/I {print}' animals.txt
i hate cats
ther is a cat
cat is not here
a cat cant exits
here is a black cAT
no he is not taking cat exam
i am not talking about dogs
[root@localhost bsurnida]# awk '/cat/i {print}' animals.txt
i hate cats
ther is a cat
cat is not here
a cat cant exits
here is a black cAT
no he is not taking cat exam
i am not talking about dogs
6. awk can take input from almost anything like a file,pipe or stdin
awk '{if($5 ~ /dhclient/) print}' /var/log/messages
[root@localhost bsurnida]# cat /var/log/messages | grep dhclient
Feb 1 22:55:42 localhost dhclient: DHCPREQUEST on eth0 to 192.168.59.254 port 67
Feb 1 22:55:42 localhost dhclient: DHCPACK from 192.168.59.254
Feb 1 22:55:42 localhost dhclient: bound to 192.168.59.132 -- renewal in 718 seconds.
Feb 1 23:07:40 localhost dhclient: DHCPREQUEST on eth0 to 192.168.59.254 port 67
[root@localhost bsurnida]# awk '{if($5 ~ /dhclient/) print}' /var/log/messages
Feb 1 22:55:42 localhost dhclient: DHCPREQUEST on eth0 to 192.168.59.254 port 67
Feb 1 22:55:42 localhost dhclient: DHCPACK from 192.168.59.254
Feb 1 22:55:42 localhost dhclient: bound to 192.168.59.132 -- renewal in 718 seconds.
Feb 1 23:07:40 localhost dhclient: DHCPREQUEST on eth0 to 192.168.59.254 port 67
7. Change the input field seperator
[root@localhost bsurnida]# awk '{if($5 ~ /syslogd/) print $3}' /var/log/messages
22:55:04
[root@localhost bsurnida]# awk '{if($5 ~ /syslogd/) print $3}' /var/log/messages | awk -F: '{print $1,$2,$3}'
22 55 04
8.Change the out put field seperator
[root@localhost bsurnida]# date | awk '{print $4}'
23:13:09
[root@localhost bsurnida]# date | awk '{print $4}' | awk -F: 'BEGIN {OFS="-";} {print $1,$2,$3}'
23-14-11
Sunday, February 1, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment