user3116123
user3116123

Reputation: 129

How to remove only the last comma in a file using Unix?

My file SQL_INPUT.csv will have the value like below (no. of values won't be necessarily 3; it will amount to even 300, but the format will be like the one below). I need to only remove the last comma:

'YC101692','YE100097','YU102391',

I have tried the below

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

but it didn't give any result (didn't remove the last ,). I also tried:

SQL_INPUT.csv | tr -d ",$"

but it removed all commas.

Upvotes: 1

Views: 5901

Answers (3)

BMW
BMW

Reputation: 45243

Is file generated in Windows, please try below:

dos2unix SQL_INPUT.csv SQL_INPUT.csv
sed '$ s/,$//' SQL_INPUT.csv

Upvotes: 0

Jotne
Jotne

Reputation: 41456

Here is an awk

awk '{sub(/,$/,x)}1' SQL_INPUT.csv
'YC101692','YE100097','YU102391'

Upvotes: 0

Mike Dinescu
Mike Dinescu

Reputation: 55720

You're attempt to use sed seemed almost right. I'd say the following should do the trick:

sed s/,$// SQL_INPUT.csv

Also, if you think there may be spaces following the last comma you could change the command above to something like this:

sed 's/, *$//' SQL_INPUT.csv

Upvotes: 2

Related Questions