awk '{ print "*" NF "\r"; for(i = 1; i <= NF; i++) { print "$" length($i) "\r\n" $i "\r" } }' sample | redis-cli --pipe
If redis is running on docker use:
awk '{ print "*" NF "\r"; for(i = 1; i <= NF; i++) { print "$" length($i) "\r\n" $i "\r" } }' sample | docker exec -i redis redis-cli --pipe
This is based on the ruby snippet described here that uses ruby String.bytesize
which is not exactly the same of awk's string length, so use this at your own risk.
For my usage this worked like a charm.
The version below supports bytesize using wc -c
, but it feels bad to perform a system call for every argument and it's also very slow.
awk '{ print "*" NF "\r"; for(i = 1; i <= NF; i++) { system("printf " $i "| wc -c | sed -e \"s/ *//\" -e \"s/^/$/\" -e \"s/$/\r/\""); print $i "\r" } }'
On Mac OS this awk's length()
seems to count the actual bytesize of a string.
$ echo -n 'Jose' | awk '{ print length($1) }'
4
$ echo -n 'José' | awk '{ print length($1) }'
5
This is awesome