Tuesday, November 3, 2009

Removing ^M in text files under Linux

As per this ASCII table the carriage return or the character ^M is listed as:
CR ^M 13 0D carriage return
^M can either be expressed in decimal or base 10 (n10) as: 13;
in hexadecimal or base 16 (n16) as: 0D;
or in octal or base 8 (n8) as: 015.

The conversion: 0158 = 08 18 58 = 0x82 + 1x81 + 5x80 = 0 + 8 + 5 = 1310

Here's a neat little tip for mass removal of the annoying presence of ^M in text files that were edited on a Windows computer in Linux using Perl:

perl -pi -e "s/\015//g" index.html
or even better:

perl -pi -e "s/\015//g" folder/*.*
removes it completely from all text files in the folder.

No comments:

Post a Comment