xargs 可以分隔参数吗?

发布于 2024-11-09 12:41:14 字数 294 浏览 3 评论 0原文

echo "'param 1' 'param 2'" | xargs -n2 -I@ echo [@] [@]

该命令输出:

[参数1参数2][参数1参数2]

但是,我希望它输出:

[参数1][参数2]

有没有办法用 xargs 来做到这一点?我计划将其与 -L1 一起使用,以便该解决方案可以处理多行以及多个参数。

echo "'param 1' 'param 2'" | xargs -n2 -I@ echo [@] [@]

This command outputs:

[param 1 param 2] [param 1 param 2]

However, I would like it to output:

[param 1] [param 2]

Is there a way to do this with xargs? I plan to use this with -L1 so the solution would handle multiple lines as well as multiple arguments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独孤求败 2024-11-16 12:41:14

对于那些通过搜索找到此内容的人来说,接受的答案对我不起作用。

echo "'param 1' 'param 2'" | xargs -n1 | xargs -I@ echo \[@\] \[@\]

产生:

[param 1] [param 1]
[param 2] [param 2]

它不满足原始发布者给出的要求,即让 xargs 读取多个实体,将它们分开,并将它们作为单独的参数发送到单个命令(OP 中的“echo”)。 Xargs 不是为此类任务设计的!

bash 答案可以工作。

p=(`echo "param1 param2"`); echo [${p[0]}] [${p[1]}]

产生:

[param1] [param2]

但该解决方案不适用于多条线。

使用 bash 将行对作为参数发送到单个命令的正确解决方案是:

(echo 'param 1'; echo 'param 2'; echo 'param 3'; echo 'param 4') | while read line1; read line2; do echo "[$line1] [$line2]"; done

生成:

[param 1] [param 2]
[param 3] [param 4]

GNU Parallel 答案确实有效,但必须创建并安装 GNU Parallel。 (Ubuntu 打包的版本不是 GNU Parallel。)

For those who find this from a search, the accepted answer did not work for me.

echo "'param 1' 'param 2'" | xargs -n1 | xargs -I@ echo \[@\] \[@\]

produces:

[param 1] [param 1]
[param 2] [param 2]

which does not meet the requirements given by the original poster to have xargs read in multiple entities, separate them, and send them to a single command ("echo" in the OP) as separate parameters. Xargs is not designed for this sort of task!

The bash answer can work.

p=(`echo "param1 param2"`); echo [${p[0]}] [${p[1]}]

produces:

[param1] [param2]

but this solution does not work with more than one line.

A correct solution with bash for sending pairs of lines as arguments to a single command is:

(echo 'param 1'; echo 'param 2'; echo 'param 3'; echo 'param 4') | while read line1; read line2; do echo "[$line1] [$line2]"; done

produces:

[param 1] [param 2]
[param 3] [param 4]

The GNU Parallel answer does work, but GNU Parallel must be make'd and installed. (The version packaged with Ubuntu is not GNU Parallel.)

深巷少女 2024-11-16 12:41:14
echo "'param 1' 'param 2'" | xargs -n1 | xargs -I@ echo \[@\] \[@\]

(在我的 shell 中,我需要转义 [],您的情况可能会有所不同)。

echo "'param 1' 'param 2'" | xargs -n1 | xargs -I@ echo \[@\] \[@\]

(In my shell I need to escape [], your mileage may vary).

波浪屿的海角声 2024-11-16 12:41:14

使用 GNU Parallel,您可以执行以下操作:

(echo 'param 1'; echo 'param 2') | parallel -N2 echo '\[{1}] \[{2}]'

观看介绍视频以了解更多信息:http://www.youtube。 com/watch?v=OpaiGYxkSuQ

With GNU Parallel you can do:

(echo 'param 1'; echo 'param 2') | parallel -N2 echo '\[{1}] \[{2}]'

Watch the intro video to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ

诗酒趁年少 2024-11-16 12:41:14

为什么要坚持xargs? bash 可以很好地处理这个问题:

p=(`echo "param1 param2"`); echo ${p[0]} ${p[1]}

why stick xargs? bash could handle this well:

p=(`echo "param1 param2"`); echo ${p[0]} ${p[1]}
橘亓 2024-11-16 12:41:14

正如这个答案中所建议的,给定一个echo

参数1参数2

参数3参数4

以下管道:

... | while read line1; read line2; do echo "[$line1] [$line2]"; done

产生:

[param1] [param2]
[param3] [param4]

如果您有一个文件 myFile.txt 包含许多行,每行有两个参数,请使用以下基于 xargs 的策略为了将参数重用为 $f$g

cat myFile.txt | xargs -n1 | while read f; read g; do echo $f $g; done

As suggested in this answer, given one echo:

param1 param2

param 3 param4

The following pipe:

... | while read line1; read line2; do echo "[$line1] [$line2]"; done

produces:

[param1] [param2]
[param3] [param4]

If you have a file myFile.txt containing many lines with two parameters on each, use the following strategy based on xargs in order to reuse the arguments as $f and $g:

cat myFile.txt | xargs -n1 | while read f; read g; do echo $f $g; done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文