Differences between revisions 2 and 12 (spanning 10 versions)
Revision 2 as of 2007-05-07 17:55:08
Size: 906
Editor: GreyCat
Comment: more methods, hyperlink, and clarify a GNUism
Revision 12 as of 2012-02-02 14:09:57
Size: 2103
Editor: Lhunath
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq52)]] <<Anchor(faq52)>>
Line 3: Line 3:
Carriage return characters (CRs) are used in line ending markers on some systems. There are three different kinds of line endings in common use:
Line 4: Line 5:
All these are from the [http://www.student.northpark.edu/pemente/sed/sed1line.txt sed one-liners page]:  *Unix systems use Line Feeds (LFs) only.
 *MS-DOS and Windows systems use CR-LF pairs.
 *Old Macintosh systems use CRs only.

If you're running a script on a Unix system, the line endings need to be Unix ones (LFs only), or you will have problems. You can check the kind of line endings in use by running:
Line 6: Line 11:
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!
cat -e yourscript
Line 11: Line 14:
If you want to remove all CRs regardless of whether they are at the end of a line, you can use {{{tr}}}: If you see something like this:
{{{
command^M$
^M$
another command^M$
}}}

then you need to remove the CRs. There are a plethora of ways to do this.

To remove them from a file, `ex` is a good standard way to do it:
Line 14: Line 26:
tr -d '\r' < dosfile ex -sc $'%s/\r$//e|x' file
Line 17: Line 29:
If you want to use the second {{{sed}}} example above, but without embedding a literal CR into your script: There are many more ways:
 * Some systems have a {{{dos2unix}}} command which can do this. Or {{{recode}}}, or {{{fromdos}}}.
 * You can also use col <input.txt > output.txt
 * In {{{vim}}}, you can use {{{:set fileformat=unix}}} to do it and save it with a ":w".
 * 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.
Line 19: Line 39:
----

Another way to check it:
Line 20: Line 43:
sed $'s/\r$//' dosfile # BASH only file yourscript
}}}
The output tells you whether the ASCII text has some CR, if that's the case. Note: this is only true on GNU/Linux. On other operating systems, the result of `file` is unpredictable, except that it should contain the word "text" somewhere in the output if the result "kind of looks like a text file of some sort, maybe".
{{{
imadev:~$ printf 'DOS\r\nline endings\r\n' > foo
imadev:~$ file foo
foo: commands text
arc3:~$ file foo
foo: ASCII text, with CRLF line terminators
Line 23: Line 54:
Some distributions have a {{{dos2unix}}} command which can do this. In vim, you can use {{{:set fileformat=unix}}} to do it. And another way to fix it:
{{{
nano -w yourscript
}}}
Type Ctrl-O and before confirming, type Alt-D (DOS) or Alt-M (Mac) to change the format.

And another way to fix it:
{{{
dos2unix filename
}}}

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

Carriage return characters (CRs) are used in line ending markers on some systems. There are three different kinds of line endings in common use:

  • Unix systems use Line Feeds (LFs) only.
  • MS-DOS and Windows systems use CR-LF pairs.
  • Old Macintosh systems use CRs only.

If you're running a script on a Unix system, the line endings need to be Unix ones (LFs only), or you will have problems. You can check the kind of line endings in use by running:

cat -e yourscript

If you see something like this:

command^M$
^M$
another command^M$

then you need to remove the CRs. There are a plethora of ways to do this.

To remove them from a file, ex is a good standard way to do it:

ex -sc $'%s/\r$//e|x' file

There are many more ways:

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

  • You can also use col <input.txt > output.txt

  • In vim, you can use :set fileformat=unix to do it and save it with a ":w".

  • 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.


Another way to check it:

file yourscript

The output tells you whether the ASCII text has some CR, if that's the case. Note: this is only true on GNU/Linux. On other operating systems, the result of file is unpredictable, except that it should contain the word "text" somewhere in the output if the result "kind of looks like a text file of some sort, maybe".

imadev:~$ printf 'DOS\r\nline endings\r\n' > foo
imadev:~$ file foo
foo:            commands text
arc3:~$ file foo
foo: ASCII text, with CRLF line terminators

And another way to fix it:

nano -w yourscript

Type Ctrl-O and before confirming, type Alt-D (DOS) or Alt-M (Mac) to change the format.

And another way to fix it:

dos2unix filename

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