管道输出用作Linux上grep的搜索规范 [英] Pipe output to use as the search specification for grep on Linux

查看:289
本文介绍了管道输出用作Linux上grep的搜索规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



举例来说:

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

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

解决方案

如果使用Bash,则可以使用反引号:

 > grep -egrep ......文件

-e 标志,并且双引号用于确保以连字符开头的最初 grep 的输出不会被解释为第二个 grep 的选项。

请注意,双引号技巧(也可以确保grep被视为单个参数)仅适用于Bash。它似乎不适用于(t)csh。



还要注意,反引号是将一个程序的输出转换为另一个程序的参数列表的标准方式。并非所有的程序都有一个方便的方式来读取(f)grep所做的参数。


How do I pipe the output of grep as the search pattern for another 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.

解决方案

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.

这篇关于管道输出用作Linux上grep的搜索规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆