Chris
Chris

Reputation: 57

Trouble using sed/regex to remove last character in string

I'm trying to remove a trailing " from all lines in a CSV. I am using RedHat Linux.

I'm able to do it successfully in VIM doing %s/"$//g

However, doing sed -e 's/"$//g' does not work (both in the terminal, and when I have it as part of a bash script). A similar call that I make to remove LEADING " from all the lines, sed -e 's/^"//g' DOES work, so I am extremely confused! Especially since it works in VIM.

Any suggestions?

Thanks!

Upvotes: 1

Views: 298

Answers (1)

pynexj
pynexj

Reputation: 20768

I suspect your file is in DOS format. Try like this:

$ cat file
line1"
line2"
line3"
$ sed -e 's/"$//' file
line1"
line2"
line3"
$ sed -e $'s/"[\r]\\?$//' file
line1
line2
line3
$

To check if the file is in DOS format, you can:

$ file file
file: ASCII text, with CRLF line terminators
$ head -n 1 file | hexdump -C
00000000  6c 69 6e 65 31 22 0d 0a                   |line1"..|
00000008
$

There are tools like dos2unix (and unix2dos) which you can use to convert it if necessary.

Vim is much smarter in dealing with different format of files so it's not surprised vim can handle it as expected.

Upvotes: 3

Related Questions