“Number” Manipulation in Grep

November 7th, 2006 liewsheng Posted in grep, Text Manipulation | Hits: 19355 | 5 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.

5 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?

  3. Good day! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on.
    Any suggestions?

    Feel free to surf to my website telecharger teamviewer (telecharger-teamviewer.blogspot.com)

  4. After looking into a number of the blog articles on your blog, I honestly like your technique of writing a
    blog. I added it to my bookmark site list and
    will be checking back in the near future. Please check out my web site as well and let me
    know your opinion.

  5. What’s Happening i am new to this, I stumbled upon
    this I have found It positively useful and it has
    aided me out loads. I’m hoping to give a contribution & aid other users like its aided
    me. Great job.

Leave a Reply