管道输出用作 Linux 上 grep 的搜索规范
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/437504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pipe output to use as the search specification for grep on Linux
提问by
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 的文件名。我尝试使用-e
第二个 grep的选项,但它也不起作用。
采纳答案by Alnitak
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.
另请注意,反引号是将一个程序的输出放入另一个程序的参数列表的标准方法。并非所有程序都像 (f)grep 那样方便地从 stdin 读取参数。
回答by Paul Tomblin
Try
尝试
grep ... | fgrep -f - file1 file2 ...
回答by Nathan Fellman
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
回答by hc2h3o2k
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 地址。
回答by mcvz
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 pattern2is the pattern in the file names and pattern1is the pattern searched for within files matching pattern2.
这里模式2是在文件名中的模式,模式1是模式搜索中的文件匹配模式2。
edit:Not strictly piping but still related and quite useful...
编辑:不是严格的管道,但仍然相关且非常有用......
回答by Jim Lahman
This is what I use to search for a file from a listing:
这是我用来从列表中搜索文件的方法:
ls -la | grep 'file-in-which-to-search'
回答by SidMuchRock
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: line 3 in test: No such file or directory
为每个未找到匹配的文件返回一行。
Am I missing something or is this just differences in my Darwin distribution or bash shell?
我是否遗漏了什么,或者这只是我的 Darwin 发行版或 bash shell 的不同之处?
回答by Vincent Jia
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]$
回答by armagedescu
You should grep in such a way, to extract filenames only, see the parameter -l (the lowercase L):
您应该以这种方式 grep,仅提取文件名,请参阅参数 -l(小写 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 上,输出的信息比仅文件名多得多。例如当你做
grep someSearch *
You will pipe to xargs info like this
您将像这样通过管道传输到 xargs 信息
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:
将上面的任何一行通过管道传递给 xargs 都是毫无意义的。但是当您执行 grep -l someSearch * 时,您的输出将如下所示:
filename1
filename2
Such an output can be passed now to xargs
现在可以将这样的输出传递给 xargs