Monday, September 13, 2010

An cute little utility

So recently I had some files that I needed to sanitize of those annoying return carriages (^M, or \r). I was originally under the impression that dos2unix would take care of this easily. For whatever reason, Ubuntu 10.04 has replaced dos2unix with fromdos, and fromdos does not do the trick. Very annoying.

So what to do? I could write my own little regular expression or Perl script to take care of this for me. I still have not mastered awk or sed (yes, go ahead and shake your heads). I did, however, find a really neat little utility that is very easy to use and does exactly what I want called tr.

Suppose I have a file dosfoo.yuck which I received from a collaborator that is full of carriage returns. To sanitize the file, I can simply run tr on the file as follows:

tr -d \r < dosfoo.yuck > foo.awesome

The -d flag stands for "delete". On the particular file I cared about when I ran this utility, the -d deleted all the the carriage returns, but now I had no more newlines in my file! Not to fear, because tr can handle this easily. Just do:

tr \r \n < dosfoo.yuck > foo.awesome

And voila! Problem solved.