problems with sed

$ echo "a,0,0,0,b,0" | sed 's/,0,/,1,/g'

gives output as

$ a,1,0,1,b,0

rather than as

a,1,1,1,b,0

how can i solve this problem

Thanks a lot in advance....

With awk:

T=b; echo "a,0,0,0,b,0" | awk -F"$T" -v T=$T '{gsub(/0/,"1",$1); print $1 T $2}'
a,1,1,1,b,0

Not sure if there are more similar lines and how they look. Maybe you need more generic code then.

echo "a,0,0,0,b,0" | sed 's/0,/1,/g'

if you really want to use only ',0,' format you need to do one more round of substitution

echo "a,0,0,0,b,0" | sed 's/,0,/,1,/g' | sed 's/,0,/,1,/g' 
1 Like

Below case does not work

T=b; echo "a,0.1,0,0,b,0" | awk -F"$T" -v T=$T '{gsub(/0/,"1",$1); print $1 T $2}'

output should be

a,0.1,1,1,b,0

but it is

a,1.1,1,1,b,0
echo "a,0,0,0,b,0" |sed 's/0,/1,/g'
echo "a,0,0,0,b,0" |sed 's/0/1/1;s/0/1/1;s/0/1/1'