Tuesday, April 06, 2010

Showing lines before and after a grep match

Today I ran running grep on the output of hg log, but only to realize that I wanted a couple of lines before and after the line which matched as well.

I am sure this is common knowledge, but I did not know how to do it, so am sharing it in the hope that someone finds it useful.


hg log | grep --before-context=2 --after-context=2 username


Running the above showed me changeset number, comments etc for all the mercurial changesets committed by the specified user.

2 comments:

Saager Mhatre said...

Haven't actually tried this, but I believe --context=4 should give you the same results. Just a shortcut if you want equal amount of context around the matched line.

Parag said...

Hi Saagar,

You are right, that too worked.