本文翻译自:Calling multiple commands with xargs
cat a.txt | xargs -I % echo %
In the example above, xargs takes echo %
as the command argument. 在上面的示例中,xargs将echo %
作为命令参数。 But in some cases, I need multiple commands to process the argument instead of one. 但是在某些情况下,我需要多个命令而不是一个来处理参数。 For example: 例如:
cat a.txt | xargs -I % {command1; command2; ... }
But xargs doesn't accept this form. 但是xargs不接受这种形式。 One solution I know is that I can define a function to wrap the commands, but it's not a pipeline, I don't prefer it. 我知道的一个解决方案是,我可以定义一个包装命令的函数,但这不是管道,我不喜欢它。 Is there another solution? 还有其他解决方案吗?
#1楼
参考:https://stackoom.com/question/tcgV/使用xargs调用多个命令
#2楼
One thing I do is to add to .bashrc/.profile this function: 我要做的一件事是将.bashrc / .profile添加到此函数:
function each() {
while read line; do
for f in "$@"; do
$f $line
done
done
}
then you can do things like 然后你可以做类似的事情
... | each command1 command2 "command3 has spaces"
which is less verbose than xargs or -exec. 它不如xargs或-exec冗长。 You could also modify the function to insert the value from the read at an arbitrary location in the commands to each, if you needed that behavior also. 您也可以修改该函数,以将读取值插入命令中的任意位置(如果需要),从而将其插入每个命令。
#3楼
With GNU Parallel you can do: 使用GNU Parallel,您可以:
cat a.txt | parallel 'command1 {}; command2 {}; ...; '
Watch the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1 观看介绍性视频以了解更多信息: https : //www.youtube.com/playlist?list=PL284C9FF2488BC6D1
#4楼
Another possible solution that works for me is something like - 对我有用的另一种可能的解决方案是-
cat a.txt | xargs bash -c 'command1 $@; command2 $@' bash
Note the 'bash' at the end - I assume it is passed as argv[0] to bash. 注意最后的“ bash”-我假设它以argv [0]的形式传递给bash。 Without it in this syntax the first parameter to each command is lost. 如果没有使用此语法,则会丢失每个命令的第一个参数。 It may be any word. 可以是任何单词。
Example: 例:
cat a.txt | xargs -n 5 bash -c 'echo -n `date +%Y%m%d-%H%M%S:` ; echo " data: " $@; echo "data again: " $@' bash
#5楼
You can use 您可以使用
cat file.txt | xargs -i sh -c 'command {} | command2 {} && command3 {}'
{} = variable for each line on the text file {} =文本文件中每一行的变量
#6楼
My current BKM for this is 我目前的BKM是
... | xargs -n1 -I % perl -e 'system("echo 1 %"); system("echo 2 %");'
It is unfortunate that this uses perl, which is less likely to be installed than bash; 不幸的是,它使用了perl,它比bash不太可能被安装。 but it handles more input that the accepted answer. 但它处理的输入要多于可接受的答案。 (I welcome a ubiquitous version that does not rely on perl.) (我欢迎不依赖perl的无处不在的版本。)
@KeithThompson's suggestion of @KeithThompson的建议
... | xargs -I % sh -c 'command1; command2; ...'
is great - unless you have the shell comment character # in your input, in which case part of the first command and all of the second command will be truncated. 太好了-除非您在输入中输入了shell注释字符#,否则第一个命令的一部分和第二个命令的所有部分都会被截断。
Hashes # can be quite common, if the input is derived from a filesystem listing, such as ls or find, and your editor creates temporary files with # in their name. 如果输入源于文件系统列表(例如ls或find),并且您的编辑器创建名称为#的临时文件,则哈希号#很常见。
Example of the problem: 问题示例:
$ bash 1366 $> /bin/ls | cat
#Makefile#
#README#
Makefile
README
Oops, here is the problem: 糟糕,这是问题所在:
$ bash 1367 $> ls | xargs -n1 -I % sh -i -c 'echo 1 %; echo 2 %'
1
1
1
1 Makefile
2 Makefile
1 README
2 README
Ahh, that's better: 嗯,这样更好:
$ bash 1368 $> ls | xargs -n1 -I % perl -e 'system("echo 1 %"); system("echo 2 %");'
1 #Makefile#
2 #Makefile#
1 #README#
2 #README#
1 Makefile
2 Makefile
1 README
2 README
$ bash 1369 $>