Convert CRLF's to line feeds on Linux

What's the best way to convert CRLF's to line feeds in files on Linux?

I've seen sed commands, but is there anything simpler?


Solution 1:

Use this command:

fromdos yourtextfile

The other way around:

todos yourtextfile

These commands are found in the tofrodos package (on most recent distributions), which also provides the two wrappers unix2dos and dos2unix that mimic the old unix tools of the same name.

Solution 2:

Use dos2unix.

dos2unix - DOS/MAC to UNIX text file format converter

dos2unix  [options] [-c convmode] [-o file ...] [-n infile outfile ...]

Options:
          [-hkqV] [--help] [--keepdate] [--quiet] [--version]

Solution 3:

I prefer perl:

perl -lne 's/\r//g; print' winfile.txt > unixfile.txt

But that's well-suited to my uses, and it's very easy for me to remember. Not all systems have a dos2unix command, but most that I work on have a perl interpreter.

Another is recode, a powerful replacement for dos2unix and iconv; it's available in the "recode" package in Debian repositories:

recode ibmpc..lat1 winfile.txt   # dos2unix
recode lat1..ibmpc unixfile.txt  # unix2dos

For awk fans:

awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt

...and sed:

sed 's/\r$//' winfile.txt > unixfile.txt

And now, only slightly-less-convoluted than deleting the CR's by hand in a hex editor, straight from one of our stackoverflow.com friends, useable with the beef interpreter (located on your friendly neighborhood Debian repository),

dos2unix in brainfuck!

,[[->+>+<<]>>>,[<-------------[+++++++++++++.>>>]<[>>----------[>+++++++++++++.-------------]<++++++++++>]<<<<[-]>>>[-<<<+>>>]]<[-]<[-]<]++++++++++.

big thanks to jk for wasting an hour of his life to write this!

Solution 4:

I think you can use tr, as well (though I have no funny format files on which to try):

tr -d '\r' < file1 > file2