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

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

    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

提交回复
热议问题