Sunday, August 7, 2016

Limiting grep output

Here's a quick way to limit output of the grep command.

Most of the time, we issue the following to find needle in the haystack:

grep needle file.txt

This prints out the matching pattern. If the output is way too long and we only need a section of it, we can use the extended grep (a.k.a. egrep) option.



Here's how to use regular expressions to instruct grep to only display 40 characters before and after the matching pattern:

grep -Eo '.{0,40}needle.{0,40}' file.txt

3 comments:

Anonymous said...



great article which conveyed a good information.awaiting for more updaates like this.
SEO Company in India
SEO Services in India
SEO Companies in India
SEO Company India
SEO Services India

Chris Topinka said...
This comment has been removed by the author.
Kpiksain said...

I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me
bu jhansi ba 2nd year result 2022