Advanced grepping
I’ve been using GNU/Linux for almost an year now. But the surprises and features that GNU programs have been showing almost always amazes me. Its like having a monster beast in hand, trained to do only a particular task. As time goes on, you forget that the beast can do much more than it was trained for.
This post is in relation to an interesting feature I found out in the GNU version of grep
I wanted to print two more lines in addition to the line containing the pattern pulled by grep. I wrote a bash script to achieve the same and later, an awk script. Hell, I was cursing grep for not having any feature like that until I read the documentation.
grep has three options -A, -B and -C that you can use to achieve the task. The -A option lets you print n number of lines after the pattern and the -B option lets you print n lines before the pattern. If you want to print the same number of lines before and after the pattern, use the -C option.
I was able to solve my problem by using grep -A 2 pattern file
Ah, the surprises that GNU/Linux has ! Still asks me to read documentation properly.
Sometimes, in harder ways!
Related
Comments
Leave a Reply