tags:

views:

47

answers:

1

Hello All,

I want to be able to take the files I found with my first grep statement, something like this for example: grep -r Makefile * And then pass the files found in that pass of grep to a second grep with something like this for example: grep {files} '-lfoo'

How do I do this? I know there must be a way.

Thank you.

+2  A: 
grep -l firstmatch * | xargs grep {} secondmatch
Ignacio Vazquez-Abrams
I'd recommend: `grep -l firstmatch | xargs grep -- secondmatch`. This will cause grep to be invoked as few times as possible. Also without the `-I {}` (or now deprecated -i) the braces won't be interpreted. Also, your command causes grep to be run for each file found by the first grep invocation and will grep for `{}` (or its substitution depending on your grep variant).
Kaleb Pederson
What do you mean by xargs?
NSA
@NSA: `xargs` is a command. It reads its standard input for a list of arguments (normally file names), and then concatenates them with the arguments of the command (in this case '`grep {} secondmatch`' to create commands that are executed. For more information, RTFM.
Jonathan Leffler
I would recommend using `grep -lZ ... | xargs -0 ...` to protect against spaces in filenames.
Dennis Williamson