Home » Questions » Computers [ Ask a new question ]

Convert CRLF's to line feeds on Linux

Convert CRLF's to line feeds on Linux

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

Asked by: Guest | Views: 248
Total answers/comments: 5
Guest [Entry]

"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."
Guest [Entry]

"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!"
Guest [Entry]

"In vi or Vim:

:%s/^V^M//g"
Guest [Entry]

"I found a very easy way… Open file with nano: ## nano file.txt

press Ctrl+O to save, but before pressing Enter press:
Alt+D to toggle betwen DOS and Unix/Linux line-endings, or:
Alt+M to toggle betwen Mac and Unix/Linux line-endings
then press Enter to save and Ctrl+X to quit."
Guest [Entry]

I prefer Vim and :set fileformat=unix. While not the fastest, it does give me a preview. It is especially useful in the case of a file with mixed endings.