(尽管word splitting在Bash中有一个特定的定义,但在本篇文章中,它表示要在空格或制表符上进行分割。)
使用此输入到xargs演示问题,
$ cat input.txt
LineOneWithOneArg
LineTwo WithTwoArgs
LineThree WithThree Args
LineFour With Double Spaces
和此Bash命令回显传递给它的参数,
$ bash -c 'IFS=,; echo "$*"' arg0 arg1 arg2 arg3
arg1,arg2,arg3
注意如何xargs -L1
将每一行单词拆分为多个参数。
$ xargs <input.txt -L1 bash -c 'IFS=,; echo "$*"' arg0
LineOneWithOneArg
LineTwo,WithTwoArgs
LineThree,WithThree,Args
LineFour,With,Double,Spaces
但是,xargs -I{}
将整个行扩展为{}
作为单个参数。
$ xargs <input.txt -I{} bash -c 'IFS=,; echo "$*"' arg0 {}
LineOneWithOneArg
LineTwo WithTwoArgs
LineThree WithThree Args
LineFour With Double Spaces
尽管在大多数情况下,这是完全合理的行为,但有时还是倾向于使用单词拆分行为(第一个xargs
示例)。
虽然可以将xargs -L1
视为一种解决方法,但是它只能用于在命令行的 end 处放置参数,从而无法表达
$ xargs -I{} command first-arg {} last-arg
与xargs -L1
。 (当然,除非command
能够以不同的顺序接受参数,如选项一样。)
在扩展xargs -I{}
占位符时,是否有任何方法可以使{}
逐行拆分?
答案 0 :(得分:1)
排序。
echo -e "1\n2 3" | xargs sh -c 'echo a "$@" b' "$0"
输出:
a 1 2 3 b
ref:https://stackoverflow.com/a/35612138/1563960
也:
echo -e "1\n2 3" | xargs -L1 sh -c 'echo a "$@" b' "$0"
输出:
a 1 b
a 2 3 b