Skip to main content

sed - remove the very last occuranceoccurrence of a string (a comma) in a file?

edited title
Link
Michael Durrant
  • 43.7k
  • 73
  • 176
  • 237

sed - remove the very last occurance of a string (a comma) in a file?

Source Link
spuder
  • 18.6k
  • 37
  • 94
  • 122

sed - remove the very last occurance of a string in file

I have a very large csv file. How would you remove the very last , with sed (or similar) ?

...
[11911,0,"BUILDER","2014-10-15","BUILDER",0,0],
[11912,0,"BUILDER","2014-10-15","BUILDER",0,0],
[11913,0,"BUILDER","2014-10-15","BUILDER",0,0],
]

Desired output

...
[11911,0,"BUILDER","2014-10-15","BUILDER",0,0],
[11912,0,"BUILDER","2014-10-15","BUILDER",0,0],
[11913,0,"BUILDER","2014-10-15","BUILDER",0,0]
]

The following sed command will delete the last occurrence per line, but I want per file.

sed -e 's/,$//' foo.csv

Nor does this work

sed '$s/,//' foo.csv