Reputation: 20145
I have a file containing list of newline separated redis keys (containing spaces). For example:
My key 1
My key 2
some other key
How do I use xargs to delete them all from redis.
I want to do something like:
cat file-with-keys | xargs -n1 redis-cli del
But it doesn't work because of the spaces.
Upvotes: 1
Views: 862
Reputation: 113834
If the input is newline-separated, use:
$ cat file_with_keys | xargs -d'\n' printf "<%s>\n"
<My key 1>
<My key 2>
<some other key>
The above illustrates the use of xargs
in a pipeline. If the source is truly a file, then cat
is not needed:
xargs -d'\n' printf "<%s>\n" <file_with_keys
As an aside, one often wants to provide xargs
with the -r
or --no-run-if-empty
option to prevent the program from running if no arguments are supplied:
xargs -rd'\n' printf "<%s>\n" <file_with_keys
The above assumes GNU xargs
. As Jonathan Leffler points out in the comments, other xargs
may not support these options. In particular, the xargs
on Mac OSX supports neither -d
nor -r
.
Upvotes: 3