gpt4 book ai didi

bash - 获取 xargs 到分词占位符 {}

转载 作者:行者123 更新时间:2023-12-03 16:51:40 32 4
gpt4 key购买 nike

(虽然 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 word-split 将每一行分成多个参数。
$ 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' "$0"

输出:
a 1 2 3 b

引用: 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

关于bash - 获取 xargs 到分词占位符 {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53348321/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com