Limiting Characters Returned by grep


2013-10-01

Sometimes I end up using grep to search among files that are compressed, or have had their whitespace removed. This is often JavaScript and most or all of the file is one long line. If you grep for something in that line, it will fill up your terminal with a ton a of text that is hard to hunt through.

To fix this, you can limit the number of characters grep returns for a match by using egrep. The command for this is

egrep -Rso '.{0,60}term{0,60}' .

That returns 60 characters on each side of the match "term" and returns the match. This makes it easier to grep for strings in very long lines or compressed files.