[ACCEPTED]-Pipe output to use as the search specification for grep on Linux-grep

Accepted answer
Score: 19

You need to use xargs's -i switch:

grep ... | xargs -ifoo grep foo file_in_which_to_search

This takes the 4 option after -i (foo in this case) and replaces 3 every occurrence of it in the command with 2 the output of the first grep.

This is the same 1 as:

grep `grep ...` file_in_which_to_search
Score: 13

Try

grep ... | fgrep -f - file1 file2 ...

0

Score: 10

If using Bash then you can use backticks:

> grep -e "`grep ... ...`" files

the 13 -e flag and the double quotes are there to 12 ensure that any output from the initial 11 grep that starts with a hyphen isn't then interpreted 10 as an option to the second grep.

Note that the 9 double quoting trick (which also ensures 8 that the output from grep is treated as 7 a single parameter) only works with Bash. It 6 doesn't appear to work with (t)csh.

Note 5 also that backticks are the standard way 4 to get the output from one program into 3 the parameter list of another. Not all 2 programs have a convenient way to read parameters 1 from stdin the way that (f)grep does.

Score: 5

I wanted to search for text in files (using 7 grep) that had a certain pattern in their 6 file names (found using find) in the current 5 directory. I used the following command:

 grep -i "pattern1" $(find . -name "pattern2")

Here 4 pattern2 is the pattern in the file names and pattern1 is 3 the pattern searched for within files matching 2 pattern2.

edit: Not strictly piping but still related 1 and quite useful...

Score: 3

This is what I use to search for a file 1 from a listing:

ls -la | grep 'file-in-which-to-search'
Score: 2

Okay breaking the rules as this isn't an 9 answer, just a note that I can't get any 8 of these solutions to work.

% fgrep -f test file

works fine.

% cat test | fgrep -f - file
fgrep: -: No such file or directory

fails.

% cat test | xargs -ifoo grep foo file 
xargs: illegal option -- i
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
             [-L number] [-n number [-x]] [-P maxprocs] [-s size]
             [utility [argument ...]]

fails. Note 7 that a capital I is necessary. If i use 6 that all is good.

% grep "`cat test`" file

kinda works in that it 5 returns a line for the terms that match 4 but it also returns a line grep: line 3 in test: No such file or directory for each file 3 that doesn't find a match.

Am I missing something 2 or is this just differences in my Darwin 1 distribution or bash shell?

Score: 2

I tried this way , and it works great.

[opuser@vjmachine abc]$ cat a
not problem
all
problem
first
not to get
read problem
read not problem

[opuser@vjmachine abc]$ cat b
not problem xxy
problem abcd
read problem werwer
read not problem  98989
123 not problem 345
345 problem tyu

[opuser@vjmachine abc]$ grep -e "`grep problem a`" b --col
not problem xxy
problem abcd
read problem werwer
read not problem  98989
123 not problem 345
345 problem tyu

[opuser@vjmachine abc]$ 

0

Score: 0

You should grep in such a way, to extract 9 filenames only, see the parameter -l (the 8 lowercase L):

grep -l someSearch * | xargs grep otherSearch

Because on the simple grep, the 7 output is much more info than file names 6 only. For instance when you do

grep someSearch *

You will pipe 5 to xargs info like this

filename1: blablabla someSearch blablabla something else
filename2: bla someSearch bla otherSearch
...

Piping any of above 4 line makes nonsense to pass to xargs. But 3 when you do grep -l someSearch *, your output 2 will look like this:

filename1
filename2

Such an output can be 1 passed now to xargs

More Related questions