Is there a quick way to convert text file from Unix line breaks to Windows ones in Unix environment? I thought there could be a standard utility.

link|improve this question

57% accept rate
feedback

3 Answers

up vote 17 down vote accepted

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.

link|improve this answer
1  
In case anyone comes across this question looking for Windows solutions, the upstream site for dos2unix is waterlan.home.xs4all.nl/dos2unix.html, and it works in Windows as well. – womble Aug 21 '11 at 3:59
feedback

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.

link|improve this answer
dos2unix made the trick, however thanks for tr utility, it could have saved so much time in the past! – alexeit May 8 '09 at 5:06
feedback

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

    :set fileformat=dos

    :wq

link|improve this answer
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
feedback

Your Answer

 
or
required, but never shown

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