Changing every other comma to point

Hi

I am working with set of data which is written in Swedish format. comma is used instead of point for decimal numbers in Sweden.

My data set is like this:

1,188,1,250,0,757,0,946,8,960
1,257,1,300,0,802,1,002,9,485
1,328,1,350,0,846,1,058,10,021
1,381,1,400,0,880,1,100,10,418

Which I want to change every other comma to point and have output like this:

1.188,1.250,0.757,0.946,8.960
1.257,1.300,0.802,1.002,9.485
1.328,1.350,0.846,1.058,10.021
1.381,1.400,0.880,1.100,10.418

Any idea of how to do that with simple shell scripting. It is fine If I do it in multiple steps. I mean if I change first the first instance of comma and then the third instance and ...

Thank you very much for your help.

Hello Johanni,

Please use code tags as per forum rules for your codes/Inputs/commands into your posts.
Could you please try following and let me know if this helps.

awk '{num=split($0,A,",");for(i=1;i<=num;i++){Q=i%2==0?".":",";VAL=VAL?VAL Q A:A};print VAL;VAL=""}'   Input_file

Output will be as follows.

1.188,1.250,0.757,0.946,8.960
1.257,1.300,0.802,1.002,9.485
1.328,1.350,0.846,1.058,10.021
1.381,1.400,0.880,1.100,10.418
 

Thanks,
R. Singh

It worked great, the only problem is it would removes zero if it is in the begining of the line. example of you script performance:

input:

0,003,0,200,0,000,0,000,0,020

Output:

003,0.200,0.000,0.000,0.020

Try

sed 's/$/,/; s/\([^,]*\),\([^,]*,\)/\1.\2/g; s/,$//' file
1.188,1.250,0.757,0.946,8.960
1.257,1.300,0.802,1.002,9.485
1.328,1.350,0.846,1.058,10.021
1.381,1.400,0.880,1.100,10.418
0.003,0.200,0.000,0.000,0.020

Work 100% correct.
Thanks

awk '{for (i=1; i<NF; i+=2) l=l $i "." $(i+1) FS; $0=l; NF=NF-1; l=""} 1' FS=, OFS=, infile