此问题已经在这里有了答案:
我正在尝试学习如何使用xargs,但尚未找到(不确定是否可能)如何将标准输入从管道的左侧传递到右侧的最后一个位置以外的其他位置;例如:
如果我想找到所有以A和B开头的文件和文件夹,我会做这样的事情:
echo "A* B*" |xargs -n 1 find -name
现在,如果我想对此进行扩展并grep其中的一些文件,理想情况是::
echo "A* B*" |xargs -n 1 find -name | xargs grep "whatImLookingFor" <placeholder_for_stdin> -r
或
echo "A* B*" |xargs -n 1 find -name | xargs someCommand withOptions <stdin_here> andSomeOtherOptions
谢谢!
[这里,我们开始复制sh
,在-r
之前插入您的参数:
xargs sh -c 'grep "whatImLookingFor" "$@" -r' _
-r
不应以结尾结尾; POSIX约定要求所有选项(例如-r
)在any参数(例如文件名)之前。 GNU工具允许您以相反的顺序进行操作,但是如果不这样做,则可以与更多版本的grep
兼容。
xargs grep -r -e "whatImLookingFor" --