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

前端 未结 9 1116
天涯浪人
天涯浪人 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: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?

提交回复
热议问题