About Linux FAQ

Browse More FAQs:

Howto: UNIX or Linux convert DOS newlines CR-LF to Unix/Linux format

Posted by Vivek Gite [Last updated: August 30, 2006]

Q. How do I convert DOS newlines CR/LF to Unix/Linux format?

A. To converts text files between DOS and Unix formats you need to use special utility called dos2unix.

DOS text files traditionally have carriage return and line feed pairs as their newline characters while Unix text files have the line feed as their newline character.

UNIX/Linux Commands

  • dos2unix (also known as fromdos) - converts text files from the DOS format to the Unix
    format
  • unix2dos (also known as todos) - converts text files from the Unix format to the DOS format.
  • sed - You can use sed command for same purpose

Task: Convert DOS file to UNIX format

Type the following command to convert file called myfile.txt:
$ dos2unix myfile.txt

However above command will not make a backup of original file myfile.txt. To make a backup of original file. The original file is renamed with the original filename and a .bak extension. Type the following command:
$ dos2unix -b myfile.txt

Task: Convert UNIX file to DOS format

Type the following command to convert file called myfile.txt:
$ unix2dos myfile.txt
$ unix2dos -b myfile.txt

Task: Convert DOS newlines (CR/LF) to Unix format using sed command

If you are using BASH shell type the following command (press Ctrl-V then Ctrl-M to get pattern or special symbol)
$ sed 's/^M$//' input.txt > output.txt

Task: Convert UNIX to DOS format using sed command

Type the following command if you are using bash shell:
$ sed 's/$'"/`echo \\\r`/" input.txt > output.txt

E-mail this to a friend      Printable version

Related Other Helpful FAQs:

Discussion on This FAQ

  1. bruce wolford Says:

    Howto: UNIX or Linux convert DOS newlines CR-LF to Unix/Linux format

    THANK YOU! to who ever wrote this. I’ve been messing around with this for quite a while. This even works on AIX’s legacy version of sed. My day has been so made by this little nugget.

  2. nigel Says:

    I had tried the suggestion from Wikipedia and that did not work. This solution worked perfectly first time. THANK YOU.

  3. John Cairns Says:

    Your UNIX to DOS sed script is incorrect if what you want is CRLF.

    $ echo | sed ’s/$’”/`echo \\\r`/” | hexdump
    0000000 0a0d

    ASCII 0/13 is decimal 013, hex 0d, octal 015, bits 00001101: called ^M, CR
    Official name: Carriage Return

    ASCII 0/10 is decimal 010, hex 0a, octal 012, bits 00001010: called ^J, LF, NL
    Official name: Line Feed

    For CRLF you want 0d0a. Your producing LFCR here.

  4. Calvin Smith Says:

    Dear John,

    Actually, the scripts are correct. Hexdump is reversing the byte order (integer format)

    Try this for example and as a simplified script:

    $ echo TEST | sed ’s/$/\r/’ | hexdump -C

    Then try it without the -C.
    You will notice that the 54’s (T) are next to each other. (ETTS)

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.