Saturday, November 26, 2011

Convert end-of-line character from DOS to UNIX

End-of-line (EOL) characters signifies the end of a line of a text (how surprising!). Just to make life more difficult, different operating systems use different EOL marker. In short, some of them use simply a line feed (LF, or '\n' in C/C++), others use carriage return (CR, or '\r' in C/C++) individually, or CR followed by LF. Unix-like systems (Unix, BSD, Linux, Mac OS X) use LF, while Windows (DOS) uses CR+LF. Thus, if you work under Linux and copy a text from a file formatted in Windows and paste it in vim, you will have empty lines after every line endings. It can be annoying. Fortunately, you can change the EOL marker of any text files easily. To do that, you need a package called flip.

#aptitude install flip

Then to convert from DOS to UNIX format:

$flip -uv textfile

Or to convert from UNIX to DOS format

$flip -mv textfile

Alternatively, you can check tofrodos and dos2unix packages, or you can open your file in Gedit and change the Line Ending option when you "Save as..." your file. The latter can be very easy if you have only one or two files. However, when you have a bunch of files, command line is just much easier. E.g., if your files have txt extension, you can convert all files by

$flip -uv *.txt

No comments:

Post a Comment