换行符分隔的xargs

是否有可能使xargs 使用换行符作为分隔符? (在Linux和OS X的bash上,如果有的话)

我知道-0可以使用,但它是PITA,因为不是每个命令都支持NUL分隔的输出。

东西沿线

 alias myxargs='perl -p -e "s/\n/\0/;" | xargs -0' cat nonzerofile | myxargs command 

应该工作。

GNU xargs (在Linux上是默认的;从OS X上的MacPorts安装findutils来获得它)支持-d ,它可以让你指定一个自定义的分隔符input,所以你可以做

 ls *foo | xargs -d '\n' -P4 foo 

在Bash中,我通常更喜欢避免xargs,因为它支持while循环。 对于你的问题, while read -ar LINE; do ...; done while read -ar LINE; do ...; done while read -ar LINE; do ...; done这个工作(记住要在LINE中使用数组语法,例如整行的${LINE[@]} )。 这并不需要任何诡计:默认情况下,只读使用\n作为行结束符。

我应该提出一个关于xargs与while-read循环的优缺点的问题… 完成!

不符合标准版本。 我有一个黑客版本,这样做 – 我知道“ find ... -print0 | xargs -0 ... ”之前就使用了它 find ... -print0 | xargs -0 ... “。 联系我,如果你想要一个副本 – 见我的个人资料。 (没有火箭科学 – 它只是做这个工作,但它不是一个完整的xargs副本。)

什么cat file | xargs | sed 's/ /\n/ig' cat file | xargs | sed 's/ /\n/ig' cat file | xargs | sed 's/ /\n/ig'这将使用标准的Linux bash工具将空格转换为换行符。