vote up 4 vote down star
1

Possible Duplicate:
How to convert a text file’s line termination from Windows/DOS to Unix

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?

flag

64% accept rate
3  
Dupe: superuser.com/questions/38744/…. The link provided in the accepted answer covers the dos2unix, perl and vi options among others. – nagul Oct 7 at 9:13
1  
This already has better answers though (so if one of these is to be closed, it should probably be that one) – Jonik Oct 7 at 12:02

closed as exact duplicate by nagul, Arjan van Bentem, Jeff Atwood Oct 11 at 8:39

This question covers exactly the same ground as earlier questions on this topic; its answers may be merged with another identical question.

8 Answers

vote up 10 vote down

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.

link|flag
1  
+1 Much more useful than the currently top-voted "Use dos2unix" answer. – Jonik Oct 7 at 10:00
1  
Yeah, even I'm voting this one up. Mine was more of a drive-by suggestion. – Ryan Thompson Oct 8 at 7:26
vote up 9 vote down

Use dos2unix?

link|flag
1  
and unix2dos for the other way 'round. – ~quack Oct 7 at 5:41
Quack, are you following me? Not that I don't appreciate it, with all the upvotes. – Ryan Thompson Oct 7 at 6:19
1  
dude, i'm ~quack. pronounce "~" as "not". :) but no, not following you, tho i do appear to run into you frequently. – ~quack Oct 7 at 7:55
1  
Consider elaborating on how to get this utility for your Linux system. At least on Ubuntu it's not installed by default (but by installing tofrodos package you get something very similar: packages.ubuntu.com/jaunty/tofrodos). – Jonik Oct 7 at 9:33
vote up 4 vote down

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!

link|flag
1  
(useless use of cat and) perl is as complicated as sed... thus you are not really answering the question but rather collecting reputation :) – akira Oct 7 at 6:28
1  
"best way" is subjective. this works best for me (i'm tons more comfortable with perl than sed). i didn't promise it would work best for you. – ~quack Oct 7 at 6:32
@akira: a question can have multiple valid answers. I use this method as well, occasionally, mostly in combination with other changes, so it is definitely a valid answer; but "use dos2unix" is definitely the more practical answer in most situations. So I think the ratings are fine. – reinierpost Oct 7 at 9:47
1  
@reinierpost: "is there something simpler than sed commands" ... why not provide some scripts written in brainfuck, just because some find that simpler than "dos2unix"? – akira Oct 7 at 10:19
@akira: if you find it simpler, please post it as an answer and enlighten the rest of us. – ~quack Oct 7 at 10:31
show 7 more comments
vote up 1 vote down

In vi or vim :%s/^V^M//g

link|flag
vote up 1 vote down

I do this on bash:

cat cr_stuffed.file | tr -d \r > no_more_crs.file
link|flag
nice. i saw another mention of tr earlier today. it's not a program that gets mentioned very often is it? – ~quack Oct 7 at 23:46
vote up 0 vote down

I prefer vim and :set fileformat=unix. While not the fastest it does give me a preview. Especially useful in the case of a file with mixed endings.

link|flag
vote up 0 vote down

If you want a GUI method, try the KATE text editor (other advanced text editors may be able to handle this too). Open the find/Replace dialog (Ctrl+R), and replace \r\n with \n. (NB you'll need to choose "Regular expression" from the drop down and deselect "Selection only" from the options.)

EDIT: Or, if you simply want to convert to Unix format, then use the menu option Tools > End of Line > Unix.

link|flag
There are text editors, such as jEdit, that can do these transformations automatically - you just tell it if you want Unix, Windows or Mac line separators. – Jonik Oct 7 at 10:24
Actually, KATE can do that too through the Tools > End of Line menu. Maybe I should have thought more laterally than answering the question exactly as it was worded - but if you know you specifically want to convert \r\n to \n then using search/replace is easier than remembering which OS uses which line ending. ;) – DisgruntledGoat Oct 10 at 23:22
vote up 0 vote down

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

tr -d \r < file1 > file2
link|flag

Not the answer you're looking for? Browse other questions tagged or ask your own question.