dos2unix???

Warren Block wblock at wonkity.com
Tue Apr 15 15:46:58 PDT 2003


On Tue, 15 Apr 2003, Peter Elsner wrote:

> #!/usr/bin/perl
> # SCRIPT: dtox
> # AUTHOR: Peter Elsner <peter at servplex.com>
> # PURPOSE: Convert Text files from DOS to UNIX (remove ^M's)
> #
>
> while (<>) {
>          if ($ARGV ne $OLDARGV) {
>                  rename($ARGV, $ARGV . '.bak');
>                  open(ARGVOUT, ">$ARGV");
>                  select(ARGVOUT);
>                  $OLDARGV=$ARGV;
>          }
>          s/^M//g;

Um... ^M would mean an M at the start of a line.  This should be

           s/\r//g;

or use the tr operator:

           tr/\r//d;

> % dtox filename
>
> A back up file is created filename.bak and then a new file is created with
> the ^M's removed...

The Perl -i and -p options make this kind of script much easier to
write and debug:

perl -i.bak -pe 'tr/\r//d'

That's the whole thing.  If you look up those options in man perlrun,
you'll see they create almost the exact loop in the program above.

A csh alias should work fine:

alias dtox perl -i.bak -pe 'tr/\r//d'

-Warren Block * Rapid City, South Dakota USA


More information about the freebsd-questions mailing list