#36 Fixing grep

Some versions of grep offer a remarkable variety of capabilities, including the particularly useful ability to show the context (a line or two above and below) of a matching line in the file. Additionally, some rare versions of grep can highlight the region in the line (for simple patterns, at least) that matches the specified pattern.

Both of these useful features can be emulated in a shell script, so that even users on older commercial Unixes with relatively primitive grep commands can enjoy them. This script also borrows from the ANSI color script, Script #11.

The Code

 #!/bin/sh # cgrep - grep with context display and highlighted pattern matches. context=0 esc="^[" bOn="${esc}[1m" bOff="${esc}[22m" sedscript="/tmp/cgrep.sed.$$" ...

Get Wicked Cool Shell Scripts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.