[Novalug] bash & grep question - best for optimizing?

Michael Stone mstone at mathom.us
Mon Nov 13 09:35:44 EST 2006


On Mon, Nov 13, 2006 at 09:29:25AM -0500, Richard Rognlie wrote:
>For portability, since not all forms of grep grok the -r flag (yes, I know
>I'm dating myself, but...) I'd even go so far as to change the command to
>
>	find DIR -type f -print0 | xargs -0 egrep WHATEVER /dev/null
>
>the /dev/null at the end of the egrep is important.   else you run the risk
>(however small) that xargs might choose to run egrep on a single file, and 
>you'd not see which file matched (if it did).   /dev/null forces the cmd 
>have at least 2 filenames which would then force the egrep to spit out the
>filename that matched.

Alternatively, you could use -H to force it to print the filename. No, 
that's not portable--but neither is find -print0|xargs -0. Probably 
better to just use -Hr if your grep supports it rather than worrying 
about portability in a one-liner.

>You're assured portability at that point.

oops. :)

Mike Stone



More information about the Novalug mailing list