How to remove last character in a string read from file

Hello,

The last character is a comma ,

I have tried the following:

sed -e 's/\,$//' filename-to-read

however - there are still commas at the end of each line...:confused:

Hi.

You don't have to escape the comma (\,) although it seems to make no difference.

The sed code you are using will not write back to the file.

If your sed has the -i option use that, otherwise write to a temporary file, and then copy the new file back over the original.

To remove multiple comma's at the end of a line, use something like:

sed -e 's/,*$//' file2
1 Like

In the file or on the screen?

Yep - that seems to have worked. Apparently my input has some spaces at the end so i had to execute it twice:

sed -e 's/ *$//g' file.out > no-space.out
sed -e 's/,*$//g' no-space.out

Thank you!

---------- Post updated at 11:54 AM ---------- Previous update was at 11:52 AM ----------

Was outputting to file. Ty.

Hi.

You could probably do that in one step.

i.e.

sed -e 's/[, ]*$//g'
1 Like

Read this too.