[Techtalk] real life kewl text-processing and regexps samples wanted

James Sutherland jas at spamcop.net
Sat Jan 10 13:48:12 EST 2004


On Sat, 10 Jan 2004 13:29:36 +0100, Daniel <lc-techtalk at nada.refused.cc> 
wrote:

> On Sat, Jan 10, 2004 at 04:36:51AM +0100, Daniel wrote:
>>   find . -type f -print0 | xargs -0 grep "thing-to-find"
>
> I have to correct that, shell programming is hard. :)
> xargs calls the utility with a maximum number of arguments. The
> default on my system is 5000. If find finds more than that, the
> utility (grep) is called several times. So, in the case that find
> finds 5001 files, and the last file matches, grep will not prefix
> the filename to the result.
>
> Solution:
>
>   find . -type f -print0 | xargs -0 grep "thing-to-find" /dev/null

Alternatively, using the -H option to grep makes it prefix each line
with the filename, even if only one filename is given. (-h does the
opposite, suppressing the filename even for multiple filenames.)

So:

find . -type f -print0 | xargs -0 grep -H "thing-to-find"

(You can also use fgrep for simpler matching, or egrep for more
complex matches, although these are often the same program now.)


James.


More information about the Techtalk mailing list