xargs 可以分隔参数吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于那些通过搜索找到此内容的人来说,接受的答案对我不起作用。
产生:
它不满足原始发布者给出的要求,即让 xargs 读取多个实体,将它们分开,并将它们作为单独的参数发送到单个命令(OP 中的“echo”)。 Xargs 不是为此类任务设计的!
bash 答案可以工作。
产生:
但该解决方案不适用于多条线。
使用 bash 将行对作为参数发送到单个命令的正确解决方案是:
生成:
GNU Parallel 答案确实有效,但必须创建并安装 GNU Parallel。 (Ubuntu 打包的版本不是 GNU Parallel。)
For those who find this from a search, the accepted answer did not work for me.
produces:
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.
produces:
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:
produces:
The GNU Parallel answer does work, but GNU Parallel must be make'd and installed. (The version packaged with Ubuntu is not GNU Parallel.)
(在我的 shell 中,我需要转义
[]
,您的情况可能会有所不同)。(In my shell I need to escape
[]
, your mileage may vary).使用 GNU Parallel,您可以执行以下操作:
观看介绍视频以了解更多信息:http://www.youtube。 com/watch?v=OpaiGYxkSuQ
With GNU Parallel you can do:
Watch the intro video to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ
为什么要坚持xargs? bash 可以很好地处理这个问题:
why stick xargs? bash could handle this well:
正如这个答案中所建议的,给定一个
echo
:以下管道:
产生:
如果您有一个文件
myFile.txt
包含许多行,每行有两个参数,请使用以下基于xargs
的策略为了将参数重用为$f
和$g
:As suggested in this answer, given one
echo
:The following pipe:
produces:
If you have a file
myFile.txt
containing many lines with two parameters on each, use the following strategy based onxargs
in order to reuse the arguments as$f
and$g
: