xargs 用于命令中间列表中的每个元素,而不仅仅是像 -L 那样结束
xargs for each element in a list in the middle of a command, not just the end like -L does
xargs 将字符串列表作为输入输入到命令中间的正确方法是什么?
例如,假设我想将所有通过一系列复杂的 "pipes" 的文件移动到主目录。像
$ ... | ... | ... | awk '{print }' | xargs -L1 mv ~/
尝试将 主目录 移动到每个 输入 ,而不是所需的顺序。
之前有人问过这方面的问题,但答案没有帮助:
Unix - "xargs" - output "in the middle" (not at the end!)
有没有办法将 xargs 输入放入命令的特定部分,而不仅仅是在末尾。
使用 GNU Parallel 看起来像这样:
$ ... | ... | ... | awk '{print }' | parallel mv {} ~/
它将运行 每个文件一个作业。更快的是:
$ ... | ... | ... | awk '{print }' | parallel -X mv {} ~/
它将在 运行 作业之前插入多个名称。
您没有提到 xargs
您使用的是什么。
如果你使用 BSD xargs
而不是 GNU xargs
,有一个强大的选项 -L
可以做你想做的事:
-J replstr
If this option is specified, xargs will use the data read from
standard input to replace the first occurrence of replstr instead
of appending that data after all other arguments. This option
will not affect how many arguments will be read from input (-n),
or the size of the command(s) xargs will generate (-s). The op-
tion just moves where those arguments will be placed in the com-
mand(s) that are executed. The replstr must show up as a dis-
tinct argument to xargs. It will not be recognized if, for in-
stance, it is in the middle of a quoted string. Furthermore,
only the first occurrence of the replstr will be replaced. For
example, the following command will copy the list of files and
directories which start with an uppercase letter in the current
directory to destdir:
/bin/ls -1d [A-Z]* | xargs -J % cp -Rp % destdir
例子
$ seq 3 | xargs -J@ echo @ fourth fifth
1 2 3 fourth fifth
seq 3 | xargs -J@ ruby -e 'pp ARGV' @ fourth fifth
["1", "2", "3", "fourth", "fifth"]
此处的 Ruby 代码 pp ARGV
会很好地打印它接收到的命令参数。我通常使用这种方式来调试 xargs
脚本。
xargs 将字符串列表作为输入输入到命令中间的正确方法是什么?
例如,假设我想将所有通过一系列复杂的 "pipes" 的文件移动到主目录。像
$ ... | ... | ... | awk '{print }' | xargs -L1 mv ~/
尝试将 主目录 移动到每个 输入 ,而不是所需的顺序。
之前有人问过这方面的问题,但答案没有帮助:
Unix - "xargs" - output "in the middle" (not at the end!)
有没有办法将 xargs 输入放入命令的特定部分,而不仅仅是在末尾。
使用 GNU Parallel 看起来像这样:
$ ... | ... | ... | awk '{print }' | parallel mv {} ~/
它将运行 每个文件一个作业。更快的是:
$ ... | ... | ... | awk '{print }' | parallel -X mv {} ~/
它将在 运行 作业之前插入多个名称。
您没有提到 xargs
您使用的是什么。
如果你使用 BSD xargs
而不是 GNU xargs
,有一个强大的选项 -L
可以做你想做的事:
-J replstr
If this option is specified, xargs will use the data read from
standard input to replace the first occurrence of replstr instead
of appending that data after all other arguments. This option
will not affect how many arguments will be read from input (-n),
or the size of the command(s) xargs will generate (-s). The op-
tion just moves where those arguments will be placed in the com-
mand(s) that are executed. The replstr must show up as a dis-
tinct argument to xargs. It will not be recognized if, for in-
stance, it is in the middle of a quoted string. Furthermore,
only the first occurrence of the replstr will be replaced. For
example, the following command will copy the list of files and
directories which start with an uppercase letter in the current
directory to destdir:
/bin/ls -1d [A-Z]* | xargs -J % cp -Rp % destdir
例子
$ seq 3 | xargs -J@ echo @ fourth fifth
1 2 3 fourth fifth
seq 3 | xargs -J@ ruby -e 'pp ARGV' @ fourth fifth
["1", "2", "3", "fourth", "fifth"]
此处的 Ruby 代码 pp ARGV
会很好地打印它接收到的命令参数。我通常使用这种方式来调试 xargs
脚本。