Converting Text Files: Linux/Unix/Mac to Dos/Windows and Dos/Windows to Linux/Unix/Mac

dosunix

It’s possible at some point in time you may find yourself working in a multiple OS environment. Either at work you may have some machines with a Windows flavor OS (Windows NT, XP, Vista, Windows 7, etc) as well as *nix type machines (including Unix, Linux, Mac OS X, etc).

Moving text files between the platforms can sometimes be problematic as the end-of-line (EOL) on Windows and *nix machines are different.

In the windows world the end of a line will have both a carriage return and a linefeed (2 bytes) while the Linux and Unix operating systems will have just a linefeed at the end-of-line for text files.

Some programs handle this OK, some don’t. If you FTP your text files between different operating systems in Ascii mode (asc) then the FTP program will transfer the file and place the correct EOL on the target machine. However if you copy a file to a thumbdrive from a Windows machine to a Linux/Unix/Mac machine then most likely the EOL on the target side will be incorrect.

Some operating systems have a dos-to-unix program that you can use to convert the files to the appropriate linefeed for the operating system. Mac OS X does not have the dos2unix program. However you can use the ‘awk’ utilitiy to either strip the carriage return from a dos/windows file or add the carriage return to a linux/unix file.

Below are two scripts that invoke awk to either convert the file to dos/windows format or linux/unix/Mac format.

The script dos2unix.awk:

awk ‘{ sub(“\r$”, “”); print }’

The script unix2dos.awk:

awk ’sub(“$”, “\r”)’

SAMPLE USAGE

Using the above script dos2unix.awk to convert a file named test.dos in DOS/Windows format and write to a file named test.txt to UNIX/Linux format:

dos2unix.awk < test.dos > test.txt

Using the above script unix2dos.awk to convert a file named test.txt in UNIX/Linux format and write to a file named test.dos to DOS/Windows format:

unix2dos.awk < test.txt > test.dos

For more information on the differences of the NewLine between operating systems, Wikipedia has an excellent article. Click Here for Wikipedia Newline Article.

You can leave a response, or trackback from your own site.

Leave a Reply

Subscribe to RSS Feed Follow me on Twitter!