“Number” Manipulation in Grep

November 7th, 2006 liewsheng Posted in grep, Text Manipulation | Hits: 19293 | 2 Comments »

“grep” will able to display the lines match with keyword or non-keyword (using option “-v”), but you may want to continue to display another few lines after/before the matched keyword:

grep -A N "keyword" filename

this will continue display N lines after the matching line. To display the N lines before matching line :

grep -B N "keyword" filename

Using the file form previous examples (Exclusive Grep), output of command below:

grep -A 3 "ates" filename

will be:

ates, 953645
btest 452345
#testing , this wrong code
#testing , this wrong code

What if you want to display 3 lines before the matching “keyword”:

grep -B 3 "ates" filename

you will get this:

testing 3456
testing, 452345
testing, 652345
ates, 953645

the next thing you may want to find out is the line number where the keyword match:

grep -n "atest" filename

this will be display:

6:ates, 953645

the “6″ is the line number where the “keyword” matched.

2 Responses to ““Number” Manipulation in Grep”

  1. I like the helpful information you provide on your articles.

    I will bookmark your blog and check again
    right here frequently. I’m slightly certain I’ll be informed plenty of new stuff proper
    here! Good luck for the next!

  2. Why viewers still make use of to read news papers when in this technological world everything is available on net?

Leave a Reply