Tuesday, 24 September 2013

Working on the grep and awk in putty


grep-    grep is used in putty for pattern matching.

The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines.



$ cat example.txt
UNIX operating system
UNIX and Linux operating system
Linux operation system

Now we want to search all lines in file example.txt which contains word UNIX but same time doesn't contain world Linux.
 
$ grep UNIX example.txt
UNIX operating system
UNIX and Linux operating system

Now to exclude all lines which contains Linux we will apply another grep command in this output with option -v to exclude matching word as shown in below grep command :


$ grep UNIX example.txt  | grep -v Linux
UNIX operating system





Read more: http://javarevisited.blogspot.com/2011/06/10-examples-of-grep-command-in-unix-and.html#ixzz2fnzlK6By


AWK is used for processing the rows and columns in a file. Awk has built in string function and associative arrays. It supports most of the operators, conditional blocks and loops available in C language.


How it works

oretail@abcd-xyz004|OID: ls -l

field1          field2  field3   field4             field5    filed6    field7        field8  filed9
-rw-rw-r--    1    oretail   orappadm         4        May      17           2012  1.abc
drwxrwxr-x   3    oretail   orappadm        256     Jun         29          2012  14159691
-rw-rw-r--    1    oretail   orappadm         4        May      17          2012  2.abc
-rw-rw-r--    1    oretail   orappadm         8       May       17          2012  3.abc

And among the 9 fields if we want to extract the field only then use the below command in unix box.

oretail@abcd-xyz|OID: awk '{print $1}' input_file
awk: 0602-533 Cannot find or open file input_file.
 The source line number is 1.



Same for column 2 ----8.

Now what I am doing in my project

After create the user changes in the Appoworx tool. I need to verify whether the count is same as per the sheet.

for that I need to connect to piand001 and login with  my credentials and password.

$ sudo su - appworx again ask for the password please provide the same. and all my user changes information present in the  u2/appworx/out

then ls -l | tail - (no of user changes done for the day )

 Now use the below command to sortout the logfiles.

$ grep -i user_name `ls -l o.RETAIL_USER_MAINT.*|grep "Sep 23"|awk '{print $9}'`|awk '{print $2}'

Meaning:  In ls -l field number 9 is file name.So it fill  get file name and grep for user_name in all the files
 of the date and the resulting output will be
o.RETAIL_USER_MAINT.6105052.00.log:export user_name="abendrok"
 in this field number 2 is user_name="abendrok"  it will print that. Space is used as separator.


Thanks & Have Fun...


No comments:

Post a Comment