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

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

提交回复
热议问题