I've come up with a jq
-based one-liner that converts a sequence of null-terminated strings to a sequence of JSON strings:
xargs -0 dash -c 'for i in "$@"; do printf %s "$i" | jq -Rs . ; done' _dummy_
I'd like to cut xargs
and/or dash
out of it if possible. Is there a way to do the same thing with just jq
?
Example usage:
# Create a new dir with some funny-named files
mkdir funny
cd funny
touch $'ABC\nDEF' $' GHI\nJKL ' ' - はじめまして - '
# Use find -print0 to list the files
find -type f -print0 |
# Convert the null-terminated lines to JSON strings
xargs -0 dash -c 'for i in "$@"; do printf %s "$i" | jq -Rs . ; done' _dummy_
Output:
"./ - はじめまして - "
"./ GHI\nJKL "
"./ABC\nDEF"
The following produces the output you've specified:
find . -type f -print0 | jq -Rs 'split("\u0000")[]'