I want to replace a sed patter match by it's ascii decimal representation as a comma-separated list. The transformation of the patter into the csv-integer sting in done inside a bash function. The function is doing its job but I struggle to integrate it into the sed call. Here is the example code:
#!/bin/bash
xx(){
##turns a string into its
##ascii-integer representation as a comma-separated list
##ignores double quotes at the start and end
n=0;o=""
for((k=1;k<$(echo $1 | wc -c);k++))
do
x=$(echo $1 | cut -b"$k")
a=$(printf '%d\n' "'$x" )
if [ $a = 34 ]
then
continue
else
n=$(($n+1))
if [ $n = 1 ]
then
o=$a
else
o=$(echo $o","$a)
fi
fi
done
o=$(echo '('$o')')
echo $o
}
##this works
xx "\"bla\""
##this does not work
y=$(echo "\"bla_x\"" | sed -e "s/bla/$(xx &)p")
The function works but the sed integration fails. From googling I got the impression that this should be in principle possible, so there might be just a small syntax mistake.
Any help much appreciated.
All the best
NB: in the final application the input to sed is a file with reg-exp patterns (e.g. "k_[[:alnum:]]{5}") to be replaced by the csv-concatenation. So eventually "xx" must bu called out of sed.