I have a csv with a weird format, I would like to change decimal values by a point and keep the comma as separator. The issue is that sometimes I have 3 commas and sometimes 4 commas.
For example:
30,-4,098511E-02
30,05,-4,098511E-02
66,7,-1,865433
I need to transform to:
30,-4.098511E-02
30.05,-4,098511E-02
66.7,-1.865433
I don't know if that's possible to do it with sed or awk.
I tried something like sed -i 's/\(([0-9] ,?){1,2}),/\1./g' P7.csv > P7_test.csv
with no success
CodePudding user response:
You can replace the last comma with a period using the script:
sed 's/,\([^,]\ \)$/.\1/' P7.csv > P7_test.csv
This invocation will modify P7.csv and write a blank file P7_test.csv which is probably not what you want (where ... is some script):
sed -i ... P7.csv > P7_test.csv
CodePudding user response:
You can try this sed:
cat file
30,-4,098511E-02
30,05,-4,098511E-02
66,7,-1,865433
42,35,18,15199
sed -E 's/^([0-9]*),([0-9])/\1.\2/; s/([0-9]),([0-9]*)$/\1.\2/' file
30,-4,098511E-02
30.05,-4,098511E-02
66.7,-1.865433
42.35,18.15199
Here ^([0-9]*),([0-9])
matches 2 digits at start separated by a ,
in capture groups 1 and 2. In replacement we place a dot between captured values.