[clue] Piping, xargs, find, grep and such

David L. Anselmi anselmi at anselmi.us
Sun Sep 16 17:30:20 MDT 2012


Sean LeBlanc wrote:
> I have a Ruby script that takes as input either:
>
> 1. 1-N command line args, assumes those are filenames, and iterates over
> those, translating the files into a readable format - writing on STDOUT.
> 2. If no args, read STDIN and do the same with that stream of data.
>
> find . -name * -print | xargs ./myscript.rb | grep "asearchstring"
>
> And that's all well and good. Now, I want to know what file(s) I get a
> hit in.

Since you don't seem to care that all the inputs go to the same output you could prepend each line 
with the file name.

Your script could send each file name to stderr before processing it.  That only really works for 
screen output and isn't the Unix Way.

Your script could send each file name to stdout before processing it and then your grep could 
include something that works on the file name lines:
  ... | egrep "(file:|asearchstring)"

Your script could produce an output file for each input file and then grep would work as usual (then 
to pipe to it you'd write the file names to stdout and go through xargs again).

HTH,
Dave


More information about the clue mailing list