获取 xargs 到分词占位符 {}
Get xargs to word-split placeholder {}
(虽然 word splitting 在 Bash 中有具体定义,但在这个 post 中它意味着 按空格或制表符拆分 。)
使用 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
可以看作是一种变通方法,但它只能用于将参数放在命令行的末尾,使其无法表达
$ xargs -I{} command first-arg {} last-arg
和 xargs -L1
。 (当然,除非 command
能够接受不同顺序的参数,就像选项一样。)
有什么方法可以让 xargs -I{}
在扩展 {}
占位符时对每一行进行分词?
有点。
echo -e "1\n2 3" | xargs sh -c 'echo a "$@" b' "[=10=]"
输出:
a 1 2 3 b
参考:
还有:
echo -e "1\n2 3" | xargs -L1 sh -c 'echo a "$@" b' "[=12=]"
输出:
a 1 b
a 2 3 b
(虽然 word splitting 在 Bash 中有具体定义,但在这个 post 中它意味着 按空格或制表符拆分 。)
使用 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
可以看作是一种变通方法,但它只能用于将参数放在命令行的末尾,使其无法表达
$ xargs -I{} command first-arg {} last-arg
和 xargs -L1
。 (当然,除非 command
能够接受不同顺序的参数,就像选项一样。)
有什么方法可以让 xargs -I{}
在扩展 {}
占位符时对每一行进行分词?
有点。
echo -e "1\n2 3" | xargs sh -c 'echo a "$@" b' "[=10=]"
输出:
a 1 2 3 b
参考:
还有:
echo -e "1\n2 3" | xargs -L1 sh -c 'echo a "$@" b' "[=12=]"
输出:
a 1 b
a 2 3 b