0121 -> 0*/1*/2*/1 using SED?

As per the title, though the number could be any length.

# current=0123
# echo $current
0123
# echo $current | sed 's/\([0-9]\)\([0-9]\)/\1*\/\2/g'
0*/123

I would understand this outcome if I'd not used the Global switch, but I was expecting (and hoping for):

0*/1*/2*/3

Any ideas what I am doing wrong?!

I've gotten it to work with the following:

# current=0123
# echo $current | sed 's/\([0-9]\)/\1*\//g;s/*\/$//'
0*/1*/2*/3

...but I'm still curious why my initial script didn't work!

This will make it clear:

% echo $current | sed 's/\([0-9]\)\([0-9]\)/ >>\1*\/\2<< /g'
 >>0*/1<<  >>2*/3<< 

Given your example you need:

echo $current | sed 's/\([0-9]\)\([0-9]\)\([0-9]\)/\1*\/\2*\/\3*\//'
0*/1*/2*/3

Thanks for your response, radoulov, but the string of numbers could be of any length, and I wanted something that would work with anything! Still, think I'm sorted now!

So your're own solution works, right?

Or, if you prefer(with ksh93,bash and zsh):

% v=123456789
% set -- $(paste -sd/<(fold -w1<(printf "%s\n" "$v")))
% printf "%s\n" "${@//\//*/}"                           
1*/2*/3*/4*/5*/6*/7*/8*/9

Or with zsh:

zsh 4.3.4% v=123456789
zsh 4.3.4% print "${${v///*/}#??}"
1*/2*/3*/4*/5*/6*/7*/8*/9

how about this one with awk !

echo "0121" | awk '{ for( i=1; i<length($0); i++ ) { printf "%d\*\/", substr($0, i, 1) } }END{ printf "%d\n", substr($0, i, 1) }'

Or just (GNU Awk only):

% echo "0121"|awk '$1=$1;1' FS= OFS="*/"
0*/1*/2*/1

Matrixmadhan

I guess this wil do

echo "0121" | awk '{for(i=1;i<=length($0);i++) {printf "%d*/",substr($0,i,1)}}'

Regards,
aajan