2010-02-01 20:05:39

HowTo: Convert Windows-style line endings to Unix-style (and vice versa)

Though there are some tools (e.g. dos2unix) available to convert between DOS/Windows (\r\n) and Unix (\n) line endings, you'd sometimes like to solve this rather simple task with tools available on any linux box you connect to. So, here are some examples - just pick the one that fits you best:

Windows to Unix:

#Shell
perl -pe '$_=~s#\r\n#\n#' < windows.txt [> linux.txt]
sed -r 's/\r$//' windows.txt [> linux.txt]

#Vim
:%s/\r$//
:set ff=unix

Unix to Windows (do you really want this?)
#Shell
perl -pe '$_=~s#\n#\r\n#' < linux.txt [> windows.txt]
sed -r 's/$/\r/' linux.txt [> windows.txt]

#Vim
:set ff=dos
Don't forget to save your file, when you're using vim.

Posted by haui | Permanent Link | Categories: linux