Steven Penny Steven Penny - 1 month ago 6
Bash Question

Convert line endings

I have been using

d2u
to convert line endings. After installing Puppy Linux I
noticed that it does not come with
d2u
, but
dos2unix
. Then I noticed that
Ubuntu is missing both by default.

What is another way to convert line endings?

Answer

Doing this with POSIX is tricky:

  • POSIX Sed does not support \r, \xD or \15

  • Even if it did, the in place option -i is not POSIX

  • POSIX Awk does support \r and \15 but not \xD

  • However Awk’s -i inplace and -l inplace syntax are not POSIX

  • d2u and dos2unix are not POSIX utilities, but ex is

  • POSIX ex does not support \r, \xD or \15

  • POSIX tr supports \r and \15 but not \xD

To remove carriage returns:

ex -bsc '%!tr -d \\r' -cx file

To add carriage returns:

ex -bsc '%s/$/\n/|%!tr \\0 \\r' -cx file
Comments