TIL: How to get a specific number of lines above and below a matched term with grep

Today I learned getting the context of a grep match isn't hard at all

Posted by Jason Merino on May 4, 2016

Back Story

I was trying to search through git logs to get a specific change that happened sometime in February to a specific file. I don’t remember how to format git logs off the top of my head so I reached for grep. Unfortunately, I then remembered that the default grep only returns 1 line per match. I finally had to break down and figure out how to get a context for my grep.

What I found was that there is a -C [number] flag you can pass to your grep which will give you the number of lines above and below your match.

The Solution

Here’s what I ended up with.

git log -- package.json | grep Feb -C 3


If you leave off the number after -C it defaults to 2 (or at least on my MacBook Pro it does).

May the 4th be with you! Or happy Star Wars day. Whichever you prefer.

jason merino

Jason Merino

Jason is a software engineer specializing in JavaScript and web technologies. An avid gardener, aspiring carpenter and a blessed family man.