cat a.txt | xargs -I % echo %

在上面的例子中,xargs使用echo %作为命令参数。但在某些情况下,我需要多个命令来处理参数,而不是一个。例如:

cat a.txt | xargs -I % {command1; command2; ... }

但是xargs不接受这种形式。我知道的一种解决方案是,我可以定义一个函数来包装命令,但我想避免这样做,因为它很复杂。有没有更好的解决方案?


当前回答

我目前的BKM是

... | xargs -n1 -I % perl -e 'system("echo 1 %"); system("echo 2 %");'

不幸的是,这使用perl,它不太可能安装比bash;但是它处理的输入比接受的答案要多。(我欢迎不依赖perl的普遍版本。)

@KeithThompson的建议

 ... | xargs -I % sh -c 'command1; command2; ...'

很好-除非您的输入中有shell注释字符#,在这种情况下,第一个命令的部分和第二个命令的全部将被截断。

如果输入来自文件系统列表,例如ls或find,并且您的编辑器创建了名称为#的临时文件,则哈希值#可能非常常见。

问题示例:

$ bash 1366 $>  /bin/ls | cat
#Makefile#
#README#
Makefile
README

哎呀,问题来了:

$ bash 1367 $>  ls | xargs -n1 -I % sh -i -c 'echo 1 %; echo 2 %'
1
1
1
1 Makefile
2 Makefile
1 README
2 README

啊,这样好多了:

$ 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 $>  

其他回答

我有一个解决这个问题的好办法。 只要写一个命令mcmd,就可以了

find . -type f | xargs -i mcmd echo {} @@ cat {} @pipe sed -n '1,3p'

mcmd的内容如下:

echo $* | sed -e 's/@@/\n/g' -e 's/@pipe/|/g' | csh

对我来说另一个可行的解决方案是——

cat a.txt | xargs bash -c 'command1 $@; command2 $@' bash

注意结尾的“bash”-我假设它作为argv[0]传递给bash。在这种语法中,如果没有它,每个命令的第一个参数将丢失。它可能是任何一个词。

例子:

cat a.txt | xargs -n 5 bash -c 'echo -n `date +%Y%m%d-%H%M%S:` ; echo " data: " $@; echo "data again: " $@' bash

使用GNU Parallel,你可以做到:

cat a.txt | parallel 'command1 {}; command2 {}; ...; '

出于安全原因,建议您使用包管理器 安装。但如果你做不到,你可以用这10秒 安装。

10秒安装会尝试做一个完整的安装;如果 失败了,个人安装;如果那失败了,一个最小值 安装。

$ (wget -O - pi.dk/3 || lynx -source pi.dk/3 || curl pi.dk/3/ || \
   fetch -o - http://pi.dk/3 ) > install.sh
$ sha1sum install.sh | grep 883c667e01eed62f975ad28b6d50e22a
12345678 883c667e 01eed62f 975ad28b 6d50e22a
$ md5sum install.sh | grep cc21b4c943fd03e93ae1ae49e28573c0
cc21b4c9 43fd03e9 3ae1ae49 e28573c0
$ sha512sum install.sh | grep da012ec113b49a54e705f86d51e784ebced224fdf
79945d9d 250b42a4 2067bb00 99da012e c113b49a 54e705f8 6d51e784 ebced224
fdff3f52 ca588d64 e75f6033 61bd543f d631f592 2f87ceb2 ab034149 6df84a35
$ bash install.sh

我更喜欢允许试运行模式的样式(没有| sh):

cat a.txt | xargs -I % echo "command1; command2; ... " | sh

管道也适用:

cat a.txt | xargs -I % echo "echo % | cat " | sh

试试这个:

git config --global alias.all '!f() { find . -d -name ".git" | sed s/\\/\.git//g | xargs -P10 -I{} git --git-dir={}/.git --work-tree={} $1; }; f'

它并行运行10个线程,并执行任何你想要在文件夹结构中全部回购的git命令。无论回购是1层还是n层。

例句:get all pull