管道输出用作Linux上grep的搜索规范

时间:2021-11-01 14:04:55

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

如何管理grep的输出作为另一个grep的搜索模式?

As an example:

举个例子:

grep <Search_term> <file1> | xargs grep <file2>

I want the output of the first grep as the search term for the second grep. The above command is treating the output of the first grep as the file name for the second grep. I tried using the -e option for the second grep, but it does not work either.

我希望第一个grep的输出作为第二个grep的搜索项。上面的命令将第一个grep的输出视为第二个grep的文件名。我尝试在第二个grep中使用-e选项,但它也不起作用。

8 个解决方案

#1


8  

If using Bash then you can use backticks:

如果使用Bash,那么你可以使用反引号:

> 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.

-e标志和双引号用于确保初始grep中以连字符开头的任何输出都不会被解释为第二个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.

请注意,双引号技巧(也确保将grep的输出视为单个参数)仅适用于Bash。它似乎不适用于(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.

另请注意,反引号是将一个程序的输出输入另一个程序的参数列表的标准方法。并非所有程序都有一种方便的方法从stdin读取参数(f)grep的方式。

#2


12  

You need to use xargs's -i switch:

你需要使用xargs的-i开关:

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.

这将在-i(在这种情况下为foo)之后采用选项,并将命令中的每次出现替换为第一个grep的输出。

This is the same as:

这与:

grep `grep ...` file_in_which_to_search

#3


10  

Try

尝试

grep ... | fgrep -f - file1 file2 ...

#4


4  

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:

我想在当前目录中的文件名(使用find找到)中具有特定模式的文件(使用grep)中搜索文本。我使用以下命令:

 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.

这里pattern2是文件名中的模式,pattern1是在匹配pattern2的文件中搜索的模式。

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

编辑:不严格管道,但仍然相关,非常有用......

#5


2  

This is what I use to search for a file from a listing:

这是我用来从列表中搜索文件的方法:

ls -la | grep 'file-in-which-to-search'

#6


1  

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.

失败。请注意,资本I是必要的。如果我使用那一切都很好。

% 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.

有点工作,它返回一行匹配的条款,但它也返回一行grep:第3行测试:没有找到匹配的每个文件的文件或目录。

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

我错过了什么,或者这只是我的Darwin发行版或bash shell中的差异?

#7


1  

I tried this way , and it works great.

我试过这种方式,效果很好。

[opuser@vjmachine abc]$ cat a
not problem
all
problem
first
not to get
read problem
read not problem

[opuser@vjmachine abc]$ cat b
not problem xxy
problem abcd
read problem werwer
read not problem  98989
123 not problem 345
345 problem tyu

[opuser@vjmachine abc]$ grep -e "`grep problem a`" b --col
not problem xxy
problem abcd
read problem werwer
read not problem  98989
123 not problem 345
345 problem tyu

[opuser@vjmachine abc]$ 

#8


-1  

I have found the following command to work using $() with my first command inside the parenthesis to have the shell execute it first.

我发现以下命令使用$()和括号内的第一个命令来让shell首先执行它。

grep $(dig +short) file

I use this to look through files for an IP address when I am given a host name.

当我获得主机名时,我用它来查看文件中的IP地址。

#1


8  

If using Bash then you can use backticks:

如果使用Bash,那么你可以使用反引号:

> 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.

-e标志和双引号用于确保初始grep中以连字符开头的任何输出都不会被解释为第二个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.

请注意,双引号技巧(也确保将grep的输出视为单个参数)仅适用于Bash。它似乎不适用于(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.

另请注意,反引号是将一个程序的输出输入另一个程序的参数列表的标准方法。并非所有程序都有一种方便的方法从stdin读取参数(f)grep的方式。

#2


12  

You need to use xargs's -i switch:

你需要使用xargs的-i开关:

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.

这将在-i(在这种情况下为foo)之后采用选项,并将命令中的每次出现替换为第一个grep的输出。

This is the same as:

这与:

grep `grep ...` file_in_which_to_search

#3


10  

Try

尝试

grep ... | fgrep -f - file1 file2 ...

#4


4  

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:

我想在当前目录中的文件名(使用find找到)中具有特定模式的文件(使用grep)中搜索文本。我使用以下命令:

 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.

这里pattern2是文件名中的模式,pattern1是在匹配pattern2的文件中搜索的模式。

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

编辑:不严格管道,但仍然相关,非常有用......

#5


2  

This is what I use to search for a file from a listing:

这是我用来从列表中搜索文件的方法:

ls -la | grep 'file-in-which-to-search'

#6


1  

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.

失败。请注意,资本I是必要的。如果我使用那一切都很好。

% 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.

有点工作,它返回一行匹配的条款,但它也返回一行grep:第3行测试:没有找到匹配的每个文件的文件或目录。

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

我错过了什么,或者这只是我的Darwin发行版或bash shell中的差异?

#7


1  

I tried this way , and it works great.

我试过这种方式,效果很好。

[opuser@vjmachine abc]$ cat a
not problem
all
problem
first
not to get
read problem
read not problem

[opuser@vjmachine abc]$ cat b
not problem xxy
problem abcd
read problem werwer
read not problem  98989
123 not problem 345
345 problem tyu

[opuser@vjmachine abc]$ grep -e "`grep problem a`" b --col
not problem xxy
problem abcd
read problem werwer
read not problem  98989
123 not problem 345
345 problem tyu

[opuser@vjmachine abc]$ 

#8


-1  

I have found the following command to work using $() with my first command inside the parenthesis to have the shell execute it first.

我发现以下命令使用$()和括号内的第一个命令来让shell首先执行它。

grep $(dig +short) file

I use this to look through files for an IP address when I am given a host name.

当我获得主机名时,我用它来查看文件中的IP地址。