1

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!

3
  • 1
    did you try sed 's/" *$//' Commented Mar 20, 2015 at 0:59
  • @AvinashRaj Yup, but I don't have trailing whitespace. I also checked in VIM to see if I have the carriage return right after the ". Definitely weird behavior... Commented Mar 20, 2015 at 1:24
  • did you check the contents of the file before running the sed command? Commented Mar 20, 2015 at 1:25

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! That did the trick! And normally VIM displays end of lines as ^M, hinting at the \r, so that is interesting.. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.