I have two sed commands that work perfectly fine when executed separately on a csv file from the command line. Both of them are designed to fix a format of decimals in one of the csv file columns and round the decimals to two.
- this one adds a 0 to the numbers with only one decimal:
sed -r -e 's/[0-9]\;[0-9]+\.[0-9]/&0/'
- this one rounds the numbers with more than two decimals:
sed -re 's/([0-9]+\.[0-9]{2})[0-9]+/\1/'
Now I would like to put them together in a sed script to be able to apply them at the same time. I have created this script named fixed_floats.sed
#!/bin/sed
s/[0-9]\;[0-9]+\.[0-9]/&0/
s/(\[0-9]+\.[0-9]{2}\)[0-9]+/\1/
When I try to execute it with this command:
sed -f fix_floats.sed titanic-passengers.csv
the output does not present any changes (the regular expressions in the sed commands are extended so I think the reason why it does not work is that I do not specify this when executing the script)
When I try to execute it with this command:
sed -E -f fix_floats.sed titanic-passengers.csv
I get the following error:
sed: file fix_floats.sed line 5: invalid reference \1 on `s' command's RHS
Any advises on how to create a sed script with extended regular expressions to be able to apply it on a csv effectively?
Original Output: (The column of interest is the 3rd one form the end of the row)
356;No;3;Vanden Steen, Mr. Leo Peter;male;28.0;0;0;345783;9.5;;S
546;No;1;Nicholson, Mr. Arthur Ernest;male;64.0;0;0;693;26.0;;S
Desired Output:
356;No;3;Vanden Steen, Mr. Leo Peter;male;28.0;0;0;345783;9.50;;S
546;No;1;Nicholson,Mr.Arthur Ernest;male;64.0;0;0;693;26.00;;S