grep replacement

Jeff Glass jjg at linus.UUCP
Thu Jun 2 00:44:18 AEST 1988


In article <470 at q7.tessi.UUCP> joey at tessi.UUCP (Joe Pruett) writes:
> >grep '(your_pattern_here)' | head -1
> 
> This works, but is quite slow if the input to grep is large.  A hack
> I've made to egrep is a switch of the form -<number>.  This causes only
> the first <number> matches to be printed, and then the next file is
> searched.  This is great for:
> 
> egrep -1 ^Subject *
> 
> in a news directory to get a list of Subject lines.

Try:

	sed -n -e '/pattern/{' -e p -e q -e '}' filename

This prints the first occurrence of the pattern and then stops searching
the file.  The generalizations for printing the first <n> matches and
searching <m> files (where n,m > 1) are more awkward (no pun intended)
but are possible.

/jeff



More information about the Comp.unix.wizards mailing list