I know how to split a string on a printable separator character; e.g. if the separator is ,
:
FOO='x,y,z'
printf "%s\n" ${(s:,:)FOO}
x
y
z
But what if the separator is a control character, such as \034
? For example
FOO=$'x\034y\034z'
I've tried putting everything I can think of between the :
's in the (s:...:)
qualifier, including \034
, $'\034'
, and several others, but nothing I've tried splits the original string.
(One solution that is not acceptable would be to perform a global substitution that replaces all occurrences of the original delimiter with, for example, ,
, and then to split the resulting string with (s:,:)
. The reason for ruling out such a solution is that the rational for using `$'\034' as delimiter in the first place is to reduce the chance that the delimited text would contain a delimiter. Replacing the original delimiter with a printable one completely defeats this rationale.)
EDIT: regarding putting the delimiter in a variable, I tried the following test script
#!/usr/bin/env zsh
DELIM=$'\034'
FOO="x${DELIM}y${DELIM}z"
BAR=(${(ps:$DELIM:)FOO})
printf "%s\n" $BAR
BAZ=(${(ps:\034:)FOO})
printf "%s\n" $BAZ
The output I get is
x^\y^\z
x
y
z
(where I've used ^\
to simulate the appearance of the printed \034
on my terminal.)
FWIW, my shell version is zsh 5.0.7 (x86_64-pc-linux-gnu)
.
p
flag is in the documentation, and I still managed to miss it. :/