Differences between revisions 2 and 3
Revision 2 as of 2007-05-07 17:55:08
Size: 906
Editor: GreyCat
Comment: more methods, hyperlink, and clarify a GNUism
Revision 3 as of 2008-05-21 14:56:55
Size: 1311
Editor: GreyCat
Comment: expand
Deletions are marked like this. Additions are marked like this.
Line 23: Line 23:
Some distributions have a {{{dos2unix}}} command which can do this. In vim, you can use {{{:set fileformat=unix}}} to do it. All of the previous examples write the modified file to standard output. Redirect the output to a new file, and then {{{mv}}} it over top of the original.

There are many more ways:
 * Some systems have a {{{dos2unix}}} command which can do this. Or {{{recode}}}, or {{{fromdos}}}.
 * In {{{vim}}}, you can use {{{:set fileformat=unix}}} to do it.
 * You can use Perl:
  {{{
  perl -pi -e 's/\r\n/\n/' filename}}}
 This has the advantage of overwriting the original file, so you don't have to mess with temporary files.

Anchor(faq52)

How do I convert a file from DOS format to UNIX format (remove CRs from CR-LF line terminators)?

All these are from the [http://www.student.northpark.edu/pemente/sed/sed1line.txt sed one-liners page]:

sed 's/.$//' dosfile              # assumes that all lines end with CR/LF
sed 's/^M$//' dosfile             # in bash/tcsh, press Ctrl-V then Ctrl-M
sed 's/\x0D$//' dosfile           # GNUism - does not work with Unix sed!

If you want to remove all CRs regardless of whether they are at the end of a line, you can use tr:

tr -d '\r' < dosfile

If you want to use the second sed example above, but without embedding a literal CR into your script:

sed $'s/\r$//' dosfile            # BASH only

All of the previous examples write the modified file to standard output. Redirect the output to a new file, and then mv it over top of the original.

There are many more ways:

  • Some systems have a dos2unix command which can do this. Or recode, or fromdos.

  • In vim, you can use :set fileformat=unix to do it.

  • You can use Perl:
    •   perl -pi -e 's/\r\n/\n/' filename
    This has the advantage of overwriting the original file, so you don't have to mess with temporary files.

BashFAQ/052 (last edited 2022-01-30 01:59:53 by larryv)