| jan2003.tar |
grep Context DisplaysThe GNU version of the grep command found on Linux, FreeBSD, and other UNIX systems, has been extended in several ways. One of these is to optionally display the context in which matching lines occur within the file. The relevant options are -A, -B, -C, each of which takes a positive integer as its argument; they cause grep to display the specified number of lines after, before, or before and after, each matching line (respectively). Here is an example illustrating how these options work:
% grep Lyta bio cats: Daphne, Susan, Lyta and Talia. % grep -B1 -A2 Lyta bio the dramatic arts shows in the names of her cats: Daphne, Susan, Lyta and Talia. Daphne named for a character in The Jewel in the Crown (her sister was named Sarah), and the other threeI got used to a context-printing search command early in my career when I worked on VMS systems. Years ago, I wrote a Perl script to perform this task, which I still use (and which also works on all the systems I currently administer). The latest version is given in Listing 1. Here is an example of its use:
% wgrep.pl -s -w1:2 Lyta bio ********** bio ********** the dramatic arts shows in the names of her * cats: Daphne, Susan, Lyta and Talia. Daphne named for a character in The Jewel in the Crown (her sister was named Sarah), and the other threeThe output is more rococo (read, VMS-like) than that from GNU grep, but it suits me. |