grep replacement

Brandon S. Allbery allbery at ncoast.UUCP
Tue Jun 14 07:51:49 AEST 1988


As quoted from <5007 at sdcsvax.UCSD.EDU> by hutch at net1.ucsd.edu (Jim Hutchison):
+---------------
| 4537 at vdsvax.steinmetz.ge.com, barnett at vdsvax.steinmetz.ge.com (Bruce G. Barnett)
| >In <1036 at cfa.cfa.harvard.EDU> wyatt at cfa.harvard.EDU (Bill Wyatt) writes:
| >|> There have been times when I wanted a grep that would print out the
| >|> first occurrence and then stop.
| >|
| >|grep '(your_pattern_here)' | head -1
| >
| >There are times when I want the first occurrence of a pattern without
| >reading the entire (i.e. HUGE) file.
| 
| I realize this is dependent on the way in which processes sharing a
| pipe act, but this is a point worth considering before we get yet
| another annoying burst of "cat -v" type programs.
| 
| grep pattern file1 ... fileN | head -1
| 
| This should send grep a SIGPIPE as soon as the first line of output
| trickles through the pipe.  This would result in relatively little
| of the file actually being read under most Unix implementations.
+---------------

Not true.  The SIGPIPE is sent when "grep" writes the second line, *not*
when "head" exits!  If there *is* only one line containing the pattern, grep
will happily read all of the (possibly large) files without getting SIGPIPE.
This is not pleasant, even if it's only one large file -- say a
comp.sources.unix posting which you're grepping for a Subject: line.
-- 
Brandon S. Allbery			  | "Given its constituency, the only
uunet!marque,sun!mandrill}!ncoast!allbery | thing I expect to be "open" about
Delphi: ALLBERY	       MCI Mail: BALLBERY | [the Open Software Foundation] is
comp.sources.misc: ncoast!sources-misc    | its mouth."  --John Gilmore



More information about the Comp.unix.wizards mailing list