How do I convert line breaks in a text file between the Windows and Unix/Linux formats?
I have a *nix environment, but that I need to import and export data with the Windows-style line breaks. I thought there would be a standard utility or command to do this, but I can't seem to find it.
You're probably looking for
dos2unix
,unix2dos
,todos
orfromdos
depending on your distribution. Ubuntu/Debian packagetodos
/fromdos
as part of the tofrodos package from memory.One option is to use
unix2dos
(anddos2unix
for going back) on the command line.Another is to use a text editor:
For vi: :set ff=dos to set the line endings to be dos line endings.
For emacs: C-x [ENTER] f dos [ENTER]
For your favourite GUI based editor (eg. jedit) I recommend checking the manual or Google.
Lastly if you don't want to deal with a text editor and just do it using more common utilities and such (or don't have unix2dos installed):
tr -d '\r' < infile > outfile
to go from Windows -> Unixawk 'sub("$", "\r")' unixfile.txt > winfile.txt
to go from Unix -> Windows astr
can not go from Unix to Windows.Edit it in Vim and use the
set fileformat
command.MS-DOS/Windows (CR+LF breaks) to *nix (LF only breaks)
*nix to MS-DOS/Windows
This is what I use, similar to Chealion, to convert Windows to Unix line endings:
Doing this with POSIX is tricky:
POSIX Sed does not support
\r
or\15
. Even if it did, the in place option-i
is not POSIXPOSIX Awk does support
\r
and\15
, however the-i inplace
option is not POSIXd2u and dos2unix are not POSIX utilities, but ex is
POSIX ex does not support
\r
,\15
,\n
or\12
To remove carriage returns:
To add carriage returns: