Problem
I want to convert a string of paths (where each path may contain escaped spaces) into a printed list of paths. I'm mainly using echo
and sed
, and the problem lies therein (see below).
For example, I want these three files:
one two three\ two\ one
To be printed on three lines:
one
two
three two one
Problem with "sed" and "echo"
(1) These are three sample files: "one", "two", and "three two one".
echo "one two three\ two\ one"
# one two three\ two\ one
(2) I replace the whitespace that separates files with a newline.
echo "one two three\ two\ one" | sed 's/\([^\]\) /\1$'"'"'\\n'"'"'/g'
# one$'\n'two$'\n'three\ two\ one
(3) I test that indeed, "echoing" the output of the above results in the output I want.
echo one$'\n'two$'\n'three\ two\ one
# one
# two
# three two one
Combining (2) and (3), the functionality breaks using "xargs echo":
echo "one two three\ two\ one" | sed 's/\([^\]\) /\1$'"'"'\\n'"'"'/g' | xargs echo
# one$\ntwo$\nthree two one
How to fix the sed substitution?
How can the sed substitution be fixed so that echoing the output gives:
one
two
three two one
I'm looking for a solution that uses primarily "echo" and "sed" (not looking for other solutions).
I also can't use "echo -e" to interpret escaped characters.