如何将 xargs 与包含空格的输入一起使用?
How do I use xargs with input containing spaces?
我有一个包含换行符分隔的 redis 键列表(包含空格)的文件。例如:
My key 1
My key 2
some other key
如何使用 xargs 从 redis 中删除它们。
我想做这样的事情:
cat file-with-keys | xargs -n1 redis-cli del
但由于空格的原因,它不起作用。
如果输入是换行分隔的,使用:
$ cat file_with_keys | xargs -d'\n' printf "<%s>\n"
<My key 1>
<My key 2>
<some other key>
以上说明了 xargs
在管道中的使用。如果源确实是文件,则不需要 cat
:
xargs -d'\n' printf "<%s>\n" <file_with_keys
顺便说一句,如果没有提供参数,人们通常想为 xargs
提供 -r
或 --no-run-if-empty
选项以防止程序 运行:
xargs -rd'\n' printf "<%s>\n" <file_with_keys
以上假定 GNU xargs
。正如 Jonathan Leffler 在评论中指出的那样,其他 xargs
可能不支持这些选项。特别是 xargs
on Mac OSX 既不支持 -d
也不支持 -r
.
我有一个包含换行符分隔的 redis 键列表(包含空格)的文件。例如:
My key 1
My key 2
some other key
如何使用 xargs 从 redis 中删除它们。
我想做这样的事情:
cat file-with-keys | xargs -n1 redis-cli del
但由于空格的原因,它不起作用。
如果输入是换行分隔的,使用:
$ cat file_with_keys | xargs -d'\n' printf "<%s>\n"
<My key 1>
<My key 2>
<some other key>
以上说明了 xargs
在管道中的使用。如果源确实是文件,则不需要 cat
:
xargs -d'\n' printf "<%s>\n" <file_with_keys
顺便说一句,如果没有提供参数,人们通常想为 xargs
提供 -r
或 --no-run-if-empty
选项以防止程序 运行:
xargs -rd'\n' printf "<%s>\n" <file_with_keys
以上假定 GNU xargs
。正如 Jonathan Leffler 在评论中指出的那样,其他 xargs
可能不支持这些选项。特别是 xargs
on Mac OSX 既不支持 -d
也不支持 -r
.