将xargs与cat和grep相结合



我想从一个文件中获取输出,并将其作为grep命令的参数输入到我要对其执行的同一文件中:

:~$ cat example
cat
dog
mouse
ant
:~$ cat example | xargs cat example | grep
Usage: grep [OPTION]... PATTERN [FILE]...
cat: cat: No such file or directory
cat: dog: No such file or directory
cat: mouse: No such file or directory
cat: antTry 'grep --help' for more information.
: No such file or directory

换句话说,预期的命令是:

cat example | grep cat
cat example | grep dog
cat example | grep mouse
cat example | grep ant

您需要像这样将cat example的输出馈送到xargs -I{},其中{}是参数占位符:

cat example | xargs -n1 -I{} grep {} example

相关内容

  • 没有找到相关文章

最新更新