grep replacement

Roy Smith roy at phri.UUCP
Sat May 28 23:59:20 AEST 1988


wyatt at cfa.harvard.EDU (Bill Wyatt) writes:
[as a way to get just the first occurance of pattern]
> grep '(your_pattern_here)' | head -1

	Yes, it'll certainly work, but I think it bypasses the original
intention; to save CPU time.  If I had a 1000 line file with pattern on
line 7, I want grep to read the first 7 lines, print out line 7, and exit.
grep|head, on the other hand, will read and search all 1000 lines of the
file; it won't exit (with a EPIPE) until it writes another line to stdout
and finds that head has already exited.  In fact, if grep block-buffers its
output, it may never do more than a single write(2) and never notice that
head has exited.

	Anyway, I agree with the "find first match" flag being a good idea.
It would certainly speed up things like

	grep "^Subject: " /usr/spool/news/comp/sources/unix/*

where I know that the pattern is going to be matched in the first few lines
and don't want to bother searching the rest of the multi-killoline file.
-- 
Roy Smith, System Administrator
Public Health Research Institute
455 First Avenue, New York, NY 10016
{allegra,philabs,cmcl2,rutgers}!phri!roy -or- phri!roy at uunet.uu.net



More information about the Comp.unix.questions mailing list