33

How do I convert line breaks in a text file between the Windows and Unix/Linux formats?

I have a *nix environment, but that I need to import and export data with the Windows-style line breaks. I thought there would be a standard utility or command to do this, but I can't seem to find it.

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
alexeit
  • 906
  • 2
  • 16
  • 19

5 Answers5

42

You're probably looking for dos2unix, unix2dos, todos or fromdos depending on your distribution. Ubuntu/Debian package todos/fromdos as part of the tofrodos package from memory.

saschabeaumont
  • 2,794
  • 22
  • 14
  • 4
    In case anyone comes across this question looking for Windows solutions, the upstream site for `dos2unix` is http://waterlan.home.xs4all.nl/dos2unix.html, and it works in Windows as well. – womble Aug 21 '11 at 03:59
  • You can install [`dos2unix`/`unix2dos`](http://waterlan.home.xs4all.nl/dos2unix.html) on Mac OS X using [Homebrew](http://mxcl.github.com/homebrew/). With Homebrew installed, the command is `brew install dos2unix`, which uses [this formula](https://github.com/mxcl/homebrew/blob/master/Library/Formula/dos2unix.rb). – Rory O'Kane Jan 20 '13 at 22:55
24

One option is to use unix2dos (and dos2unix for going back) on the command line.

Another is to use a text editor:
For vi: :set ff=dos to set the line endings to be dos line endings.
For emacs: C-x [ENTER] f dos [ENTER]

For your favourite GUI based editor (eg. jedit) I recommend checking the manual or Google.

Lastly if you don't want to deal with a text editor and just do it using more common utilities and such (or don't have unix2dos installed):

tr -d '\r' < infile > outfile to go from Windows -> Unix
awk 'sub("$", "\r")' unixfile.txt > winfile.txt to go from Unix -> Windows as tr can not go from Unix to Windows.

Chealion
  • 5,733
  • 28
  • 29
  • dos2unix made the trick, however thanks for tr utility, it could have saved so much time in the past! – alexeit May 08 '09 at 05:06
  • If a file has mixed line endings, make sure to load it in proper `ff`: `:e ++ff=unix`. At least it makes sense for `dos -> unix` conversion. – x-yuri Sep 17 '14 at 12:40
  • That awk one liner is teaching a man to fish, thanks for that. I'll be slightly less hungry in future – Gareth Davidson Dec 04 '14 at 18:35
13

Edit it in Vim and use the set fileformat command.

  • MS-DOS/Windows (CR+LF breaks) to *nix (LF only breaks)

    :set fileformat=unix
    :wq
    
  • *nix to MS-DOS/Windows

    :set fileformat=dos
    :wq
    
Alois Mahdal
  • 283
  • 1
  • 4
  • 18
nedm
  • 5,630
  • 5
  • 32
  • 52
  • Be careful when using that if the file does not already have a line break at the last line; vim will add one unless you know how to tell it not to. – CesarB Jun 14 '09 at 20:54
  • After hours of trying every other solution this was the lifesaver I needed. Confirmed behaviour on OS X Yosemite with `hexdump -C` -- also confirmed this is *not* sticky -- it is set per `vi` session – Techmag Oct 15 '15 at 20:34
8

This is what I use, similar to Chealion, to convert Windows to Unix line endings:

tr -d \\015 < windows > unix
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
Josh
  • 337
  • 2
  • 10
  • The poster wanted unix to windows line endings. And you're some years late with you answer. – ott-- Oct 07 '12 at 18:36
  • 5
    Who cares if it's "years late"? It still works, and it's still applicable to the question. I was just adding another option for readers that come upon it, like myself. Also, his question asks for Windows -> Unix, and his description asks for Unix -> Windows. *shrug* – Josh Oct 09 '12 at 10:57
  • Right. Maybe you add the other tr-command for converting unix to windows then. – ott-- Oct 09 '12 at 16:53
  • I tried to undo the downvote, but it said that it's locked until the article is edited. – ott-- Oct 09 '12 at 21:09
  • @ott-- no problem, I upvoted for you. ...wait, now *I* can't upvote for me! ...dang... :) – Alois Mahdal Jul 22 '13 at 00:13
1

Doing this with POSIX is tricky:

  • POSIX Sed does not support \r or \15. Even if it did, the in place option -i is not POSIX

  • POSIX Awk does support \r and \15, however the -i inplace option is not POSIX

  • d2u and dos2unix are not POSIX utilities, but ex is

  • POSIX ex does not support \r, \15, \n or \12

To remove carriage returns:

awk 'BEGIN{RS="\1";ORS="";getline;gsub("\r","");print>ARGV[1]}' file

To add carriage returns:

awk 'BEGIN{RS="\1";ORS="";getline;gsub("\n","\r&");print>ARGV[1]}' file
Zombo
  • 1
  • 1
  • 16
  • 20