[Novalug] grep usage request

David Durant (emags) emags4DLD@cox.net
Wed May 7 10:43:33 EDT 2014


On 05/07/2014 09:51 AM, Michael Henry wrote:
> On 05/07/2014 09:36 AM, Richard Rognlie wrote:
>> find /home/chowe -type f -print0 | xargs -0 grep "1Adam P" /dev/null
>>
>> Why do I have the /dev/null at the end, you ask?
> [...]
>> Sure, there's probably a cmdline switch in grep for that, but
>> I can never remember it.
Yes -- from the 'man grep' pages:

Output Line Prefix Control

        -H, --with-filename
               Print the file name for each match.  This is the default 
when there is more than one file to search.

        -h, --no-filename
               Suppress the prefixing of file names on output.  This is 
the default when there is only one  file  (or  only  standard input)  to
               search.
> With GNU xargs, you can avoid the case that grep will be invoked
> with no filename arguments via the -r (--no-run-if-empty) switch.
> >From man xargs:
>
>         -r, --no-run-if-empty
>                If the standard input does not contain any
>                nonblanks, do not run the command.  Normally, the
>                command is run once even if there is no input.
>                This option is a GNU extension.
>
>    find /home/chowe -type f -print0 | xargs -0r grep "1Adam P"
>
> Michael Henry
>
> _______________________________________________
> Novalug mailing list
> Novalug@calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/novalug




More information about the Novalug mailing list