Saturday 28 January 2012

awk

                                                                              
awk
awk is very good programming language/command  in manipulating files which have patterns separated by space or other delimiters. It does these  operations  faster than other basic  unix commands like sed and grep.
awk’s power lies in its “one line” editing option which makes it more a command than a programming language.
Examples:
Example1)
suppose that you have a file which contains 4 columns of an SQL  spool file.
/home/b3456/$ cat  member_list.txt                         (Note: here /home/b3456/$  is the prompt string)
Ajit kumar                1000             BLR
sachin Sharma        2500             MUM
Ajay gupta                6560             MUM
Srinivasan   R          4509              NDL
Ahmed  javed          7845              CHE


To print only the city codes(third field or column ) enter the following  command.
/home/b3456/$ awk  ‘{print $3}’   member_list.txt
BLR
MUM
MUM
NDL
CHE

Example 2)
 you want to get the member whose amount withdrawn ( 2nd  column)  is greater than  or equal to 6000 and less than 7000 ,enter the command
/home/b3456/$ awk  ‘  {if ( $2  >=  6000 && $2  < 7000) print $1 }’    member_list.txt
Ajay gupta                
Example 3)
 To get the total size of a group of files  in  a directory.
/home/b3456/$  ls  –lrt
 rw-r--r--    1 b3456  Administ    19738       Feb  1  2011  NOV_2010_3.pdf
-rw-r--r--    1 b3456  Administ    18176      Feb  1  2011  DEC_2010.pdf
--rw-r--r--    1 b3456  Administ   739840   Jul  8 23:09    2011_ITR1_r2.xls
-rw-r--r--    1 b3456  Administ    11455     Jul  8 23:13     ITR1_AJTPN1033E.xml
-rw-r--r--    1 b3456  Administ    94058     Jul  9 14:08     09072011020807_13102006
-rw-r--r--    1 b3456  Administ      101        Jul  9 14:10     address_TAX.txt
-rw-r--r--    1 b3456  Administ    43237     Jul  9 14:17     XXXPN1033X_ITR-V.zip
-rw-r--r--    1 b3456  Administ  1046954  Nov 21 14:29 EDU_LOAN_STMT.pdf
Enter the following command.

/home/b3456/$  ls  -lrt | awk ' BEGIN  {c = 0}{ c  += $5 } END{print  c}' 
973559
Note that you don’t have to use ‘$’ symbol for printing a variable .

Example 4)
Many a times ,you may want to pass a shell variable to awk  in the command line or inside
Shell-script and do some conditional manipulations based on the variable .
Consider  a situation where you need to compare  a name variable of the shell with the names in the file member_list.txt.
The script
#!/usr/bin/sh
……………..#lines of commands
……………..
p_name=`echo $m_name`         #p_name Is a shell variable.
awk   -v    myp_name=$ p_name   ‘{ if  ( $1 == myp_name)  print  $0 }’   member_list.txt.
…………….
Here the awk command prints the entire line which contains the name field same as that of value of variable p_name


  Example 5)
A  single space or a  sequence  of  space  characters is the  default delimiter   for fields, if there are any other use  awk   with option –F and mention the delimiter.
If the file member_list2.txt contained  the following,
/home/b3456/$ cat  member_list2.txt
Ajit kumar|1000|BLR
sachin Sharma|2500|MUM
Ajay gupta|6560|MUM
Srinivasan R|4509|NDL
Ahmed javed|7845|CHE
Then  to print only the city codes(third field or column ) as in example 1, awk can be used as follows.

/home/b3456/$ awk  -F  “|”  ‘{ print $3 }’    member_list2.txt
BLR
MUM
MUM
NDL
CHE












No comments:

Post a Comment