grep replacement
Jim Hutchison
hutch at net1.ucsd.edu
Sun Jun 5 09:37:17 AEST 1988
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
>
[...]
>
>Have you ever waited for a computer?
No, never. :-)
>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.
I would agree that it is a bad thing to rely on the granularity of
a pipe. Here is a sample program which can be used to show you what
I mean.
Name it grep, and use it thus wise:
% ./grep pattern * | head -1
/* ------------- Cut here --------------- */
#include <stdio.h>
#include <signal.h>
sighandler(sig)
int sig;
{
if (sig == SIGPIPE)
fprintf(stderr,"Died from a SIGPIPE\n");
else
fprintf(stderr,"Died from signal #%d\n", sig);
exit(0);
}
main()
{
signal(SIGPIPE,sighandler);
for (;;)
printf("pattern\n");
}
/* Jim Hutchison UUCP: {dcdwest,ucbvax}!cs!net1!hutch
ARPA: Hutch at net1.ucsd.edu
Disclaimer: The cat agreed that it would be o.k. to say these things. */
More information about the Comp.unix.wizards
mailing list