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

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

提交回复
热议问题