bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getting 'grepped' files from file list


From: Bob Proulx
Subject: Re: getting 'grepped' files from file list
Date: Tue, 16 Jun 2009 13:20:27 -0600
User-agent: Mutt/1.5.18 (2008-05-17)

Dave B wrote:
> Bob Proulx wrote:
> > Dave B wrote:
> >> xargs grep pattern < filelist.txt
> >> (with the usual issues to consider when using xargs)
> > 
> > You can avoid using xargs here if you don't want to use it.
> >   grep PATTERN $(<filelist.txt)
> 
> Yes that's true. I just felt that xargs (well, GNU xargs to be honest) gives
> you a bit more control in case the filelist contains filenames with spaces,
> wildcards or other oddities, which will cause problems with the
> $(<filelist.txt) approach, since the shell does word splitting and pathname
> expansion on the result of that.

I was simply reacting to the "usual issues" statement.  (shrug) Yes,
whitespace in filenames may be a problem.  But then xargs doesn't help
there unless you use zero terminated strings.  Using newline
terminated strings with xargs without options, I think, is the same as
using the $(<file) way of doing things.  At least it has very close to
the same set of problems.

Of course the traditionalist simply avoids any cases with whitespace
in filenames.  Then it won't matter.  :-)

> xargs -d '\n' grep pattern < filelist.txt
> should take care of most of the issues.

Uhm, well, hmm...  Once you start worring about whitespace in
filenames handling spaces but not handling newlines just doesn't feel
right.  Yes that will handle spaces but then not files with newlines
in them.  If effort is going to be put in there to handle one then the
other is not too far away using zero terminated strings.

Even though it is a little painful it does seem best to use zero
terminated strings for filename data.  Oh well.

> It does require GNU xargs however, as I said.

Agreed.  Because then you can use xargs --null.  The only thing I
preferred otherwise was the use of GREP_OPTIONS which is just too
global for my taste.  Otherwise I think we are in general agreement.

And I am compelled to point out that if you use zero terminated
strings then the shell can also handle them too.  Not in a standard
way though/either.  I was having fun with this test case.

$ echo one > "x y  z"
$ echo two > "a b
> c"
$ printf "x y  z\000a b\nc\000" > filelist
$ xargs -r0 grep one < filelist
x y  z:one
$

$ while read -d '' f;do printf -- "%s\n" "$f";done < filelist 
x y  z
a b
c
$

$ while read -d '' f;do grep -H two "$f";done < filelist 
a b
c:two
$

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]