Tuesday, 15 May 2012

linux - How do I use the filenames output by "grep" as argument to another program -


I have this grep command which outputs the names of files (in which some patterns Matches), and I want to parse those files with some file-parsing program The pipeline looks like this:

  grep -rl "{some-pattern}" | {Some file-parsing-program} & gt; A.out   

How do I obtain those files as command line arguments in the file-parsing program? For example, suppose that returns grep filenames a , b , c . How can I pass file names so that I can

  {some-file-parse-program} a b c & gt; A.out   

?

It seems to me that you want to:

  grep - Rl "{some_pattern" | Your command & gt; A.out   

I'm not sure a.out is a good output file name, but we can give that slide. The xargs command reads the filenames separated by the white space from the standard input, and then they call your-command as those names. It may have to invoke your-command many times; Unless you are using GNU xargs and you specify -r , your-command used at least once Even if there is no matching file name Without the use of

xargs , you could not use sed for this task. Without using xargs , Pearl (and Python) will be clumsy using the awk to 'trivialize' it; It will be easy to type the code to read the file name from standard input and then process each file in return.

No comments:

Post a Comment