Pipe output to use as the search specification for grep on Linux

前端 未结 9 1128
天涯浪人
天涯浪人 2021-02-03 22:12

How do I pipe the output of grep as the search pattern for another grep?

As an example:

grep   | xargs grep          


        
相关标签:
9条回答
  • 2021-02-03 22:36

    You need to use xargs's -i switch:

    grep ... | xargs -ifoo grep foo file_in_which_to_search
    

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

    This is the same as:

    grep `grep ...` file_in_which_to_search
    
    0 讨论(0)
  • 2021-02-03 22:37

    If using Bash then you can use backticks:

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

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

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

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

    0 讨论(0)
  • 2021-02-03 22:40

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

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

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

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

    0 讨论(0)
  • 2021-02-03 22:43

    Try

    grep ... | fgrep -f - file1 file2 ...
    
    0 讨论(0)
  • 2021-02-03 22:46

    Okay breaking the rules as this isn't an answer, just a note that I can't get any 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 that a capital I is necessary. If i use that all is good.

    % grep "`cat test`" file
    

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

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

    0 讨论(0)
  • 2021-02-03 22:46

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

    grep -l someSearch * | xargs grep otherSearch
    

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

    grep someSearch *
    

    You will pipe to xargs info like this

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

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

    filename1
    filename2
    

    Such an output can be passed now to xargs

    0 讨论(0)
提交回复
热议问题