8

Possible Duplicate:
Remove carriage return in Unix

I am reading some data generated by an external third party. I have noticed that the ASCII text in the file is interspersed with ^M characters, which I believe is character 13 in ASCII and represents a carriage return without linefeed.

Is there a one liner I can use to strip the ^M characters from the file?

I am running on Linux (Ubuntu).

anubhava
  • 761,203
  • 64
  • 569
  • 643
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341

2 Answers2

16

You can use sed like this:

sed -i.bak 's/^M$//' infile.txt

To type ^M, you need to type CTRL-V and then CTRL-M.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thank you. This is all I needed. I am guessing that infile.txt is the file to be cleansed ?, not sure what i.bak is though, perhaps, I'll go look in the manual, unless you are gracious enough to explain ..? – Homunculus Reticulli Nov 27 '12 at 17:48
  • @HomunculusReticulli: You're most welcome. `-i` flag in sed is used for inline editing of a file and `-i.bak` is used for saving the original input file with `.bak` extension for safety measure. – anubhava Nov 27 '12 at 18:02
  • The `-i.bak` is a GNU `sed` extension that means 'do in-place alter of the file, backing up the original with the `.bak` extension'. Reading the manual is still a good idea, though. – Jonathan Leffler Nov 27 '12 at 18:02
9

OR

dos2unix infile.txt file2.txt ....

OR

man dos2unix 

for more details.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • dos2unix works if it is available but unfortunately it is not available to all flavors of Unix/Linux. – anubhava Nov 27 '12 at 17:51
  • I only have experience with RH linux, but I thought all versions of Linux had `dos2unix` (Lots of old-line unix experience!). As the user specified Linux I think its a useful suggestion. Not all `sed`s support `-i` after all ;-). Thanks and good luck to all. – shellter Nov 27 '12 at 18:00
  • eg dos2unix isn't available on my Mac and if you note `-i` is optional in my answer. A simple `sed 's/^M$//' infile.txt > output.txt` should also work. – anubhava Nov 27 '12 at 18:06
  • I don't see that `-i` is optional in your answer. Not looking for a fight. Just to recognize that in unix too, there's more than one way to do it ;-> . Good luck to all. – shellter Nov 27 '12 at 18:11
  • It is obvious that text replacement in sed is being handled by `'s/^M$//'` and `-i` is just an optional and convenient shortcut. – anubhava Nov 27 '12 at 18:14
  • dos2unix now available on macOS... – jtlz2 Aug 04 '17 at 09:29