Delete trailing commas in line

Re the command you have provided:

grep "5628" test.csv | sed 's/,*$//g'

This will output lines matching '5628' with all trailing commas removed. It will not update the file test.csv.

However, you indicated the file came from a Windows machine, so the line endings are CR/NL instead of just NL. The result is that there is a hidden CR at the end of the line, and you need a command line this instead:

grep "5628" test.csv | sed 's/,*\r*$//'

Actually, you can simply this to one command:

sed -n '/5628/s/,*\r*$//p' test.csv

sed -n '/5628/ s/,*\r*$//p' test.csv

(not the OP that only ask to remove trailing coma) this avoid a piping process, sed taking directly the filter and the conversion

Tags:

Sed