Tuesday Tiny Techie Tip

negative grep

Normally, we use grep to find lines in a file or stream which match a pattern. But sometimes we want to find lines that don't match a pattern. grep's authors anticipated this need and provided the "-v" flag to grep.

If the file foo contains the lines:

% cat foo
I don't want to see the lines which
contain boring words because I don't
have time for boring stuff.  Please don't
bore me.
I can use grep -v to get rid of the boring lines like so:
% grep -v boring foo
I don't want to see the lines which
bore me.
This is especially useful for people who like to keep a bunch of comments in their clearcase config spec. It's hard to visually pick out what lines in:
% ct catcs
# my checked out files
element * CHECKEDOUT
# work on spr 11111
element * .../SPR11111/LATEST
# work on spr 22222
#element * .../SPR22222/LATEST
# grab the 3.1 bugfix stuff
#element * .../3.1_bugfixes/LATEST
# grab the latest useful 3.1 label
#element * 3.1.102
# main was really good back in july
#element * /main/LATEST -time 12-jul-96.03:00
element * /main/LATEST
are actually being used.

grep -v to the rescue:

% ct catcs | grep -v ^#
element * CHECKEDOUT
element * .../SPR11111/LATEST
element * /main/LATEST

Tuesday Tiny Techie Tip -- 5 November 1996
Forward to (11/12/96)
Back to (10/29/96)
Written by Jeff Youngstrom

Up to the TTTT index

Tuesday Tiny Techie Tips are all © Copyright 1996-1997 by Jeff Youngstrom. Please ask permission before reproducing any of this material.