Wednesday 7 November 2012

Print previous , present and next line of a line matching pattern

Here is the link from great jadu , my friend and master of sed,awk and bash scripting. 


http://unstableme.blogspot.in/2008/05/print-currentnextprevious-line-using.html    

 Q. Print present line and previous line to the pattern that matches  "Sriram" word below using awk:

 Exercise 1:
 cat sample.txt

 Jadu previous line
Sriram present line
Varun next line
 Gowda some line
 Prasahanth some line

Output should be :

 Jadu previous line
Sriram present line
Varun next line

Answer: You can do that using grep command, which supports  -A and -B options

grep -B1 -A1  -i  "sriram"  sample.txt

Refer to "sed" solution given by jadu  in the link above.

No comments:

Post a Comment

Tweets by @sriramperumalla