Convert DOS file to UNIX format

Test file from dos/windoz is annoyingwhen working at linux. The newline character must be converted. Here are a collection of fixes:

Use of vi at linux

open a text file with vi, use this commad (use press Ctrl-V then Ctrl-M to get ^M)

%s/^M//g

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

Convert UNIX file to DOS format

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

Convert Dos TO Unix Using tr Command

Type the following command:

tr -d '\r' < input.file > output.file

Convert Dos TO Unix Using Perl One Liner

Type the following command:

perl -pi -e 's/\r\n/\n/g' input.file

in perl script, s/\r\n/\n/ does not work. 
I use s/^M// (press Ctrl-V then Ctrl-M to get pattern)
, but if this is used along with chomp, it may remove more fields then necessary.

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
Note: sed version may not work under different UNIX/Linux variant,refer your local sed man page for more info.

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

About these ads

4 responses to “Convert DOS file to UNIX format

  1. Pingback: Convert DOS file to UNIX format « Linux Toys·

  2. Pingback: Convertir un fichero de formato DOS a formato UNIX « Ubuntu Life·

  3. Good day I was fortunate to come cross your website in baidu
    your Topics is wonderful
    I obtain a lot in your website really thanks very much
    btw the theme of you site is really fine
    where can find it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s