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

1 comment:

Shaakshi said...

After looking into a handful of the blog articles on your site, I really like your technique of writing a blog. I book marked it to my bookmark site list and will be checking back in the near future. Take a look at my website as well and let me know your opinion.
Car Wash Services in Mumbai