Wednesday, May 12, 2004

Stripping the 'extra' carriage return from the end of lines

Ever unzipped/tarred something on UNIX and ended up with a whole directory structure filled with files all containing ^M codes at the end of each line? The reasons are pretty much uninteresting, and well documented (Here for example). Of course, understanding why they show up doesn't really help you once you have hundreds of files already transferred to some host. On UNIX systems (Solaris in particular, ymmv on others) you can remove the ^M characters with a single find + perl command.

find . ! -type d -exec perl -pi -e 's/^M//g' \{\} \;

Remember to use ctrl-v,ctrl-m to insert the actual control-character for carriage return, not the literal string of a '^' character + an 'M' character.

No comments: