Ubuntu – the sed comand to fix this file so last value on each line is double quoted

regexsedtext processing

I have a file containing two million lines of the form:

"00005cea-668e-4475-9e19-92a25c8b74fb",129.24728",D#

the last value should actually be:

"00005cea-668e-4475-9e19-92a25c8b74fb",129.24728,"D#"

Please , how do I use sed command to fix this file so the spurious " is removed and the last value is double quoted

Best Answer

You could try something like:

sed -r 's/",([^,]*)$/,"\1"/' input-file

That's a ", followed by anything that's not a comma ([^,]) till the end of the file $. \1 is the part matched by in the parentheses - ([^,]*).