How Would I Write This Vim Command for `Sed`?

I have a .csv with special characters that are wrecking an import I need to do.

In VIM, I discovered away to remove the special characters with :

:%s/\r//g

This removes ^M from some of my broken lines.

But I'd like to either write this into a bash script, or into my ruby script. So I was curious if there's a way I can perform this special character search and replace in either Bash or Ruby.

Searching for ^M does not work. And also you wouldn't see this character in any text editor except for VIM.

6

1 Answer

This works for me (Fedora 20, GNU sed 4.2.2):

sed -e 's/\r$//' <file-in> > <file-out>

(Replace <file-in> and <file-out> with your file names.) I use file to check the line endings.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest