Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 15 Apr 2003 16:46:56 -0600 (MDT)
From:      Warren Block <wblock@wonkity.com>
To:        Peter Elsner <peter@servplex.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: dos2unix???
Message-ID:  <20030415163837.R20819@wonkity.com>
In-Reply-To: <5.2.0.9.2.20030415085340.01bae4a0@mail.servplex.com>
References:  <5.2.0.9.2.20030415085340.01bae4a0@mail.servplex.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 15 Apr 2003, Peter Elsner wrote:

> #!/usr/bin/perl
> # SCRIPT: dtox
> # AUTHOR: Peter Elsner <peter@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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030415163837.R20819>