Live Chat!

“Number” Manipulation in Grep

* * *     3 votos

November 7th, 2006 liewsheng Posted in Text Manipulation, grep | Hits: 5681 |

“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.

Leave a Reply