Phoenix.pm: Mac/Windows -> Unix line endings

Kevin Buettner kev at primenet.com
Fri Nov 5 11:51:28 CST 1999


On Nov 5, 10:12am, Douglas E. Miles wrote:

> This is a little script that I use to convert Mac/Windows format files
> to Unix format:

Sometimes you want to convert all the files in a given directory
hierarchy.  I wrote the following script which'll find all the text
files and convert them to have unix line endings.  The original files
are preserved as *.bak-<NUMBER> where <NUMBER> is a a representation of
the date as given by time().  (Do a "rm -f `find .  -name '*.bak-*'`"
when you want to get rid of the backup files.)

Regarding the following line:

    s/\015\012?/\012/g;

I wrote it this way for portability.  I read in "The Perl Journal"
that Perl on some non-unix platforms will convert \n and the like to
platform specific equivalents.  Written in the above fashion, you
should be able to run it on a Mac or on some version of Windows and
have it work the same way as on Unix.  This could be useful if you are
forced to do development on a non-unix system, but still need to
periodically check your code into a unix based source respository.

--- fix-newlines ---
#!/usr/bin/perl -w

use File::Find;
use FileHandle;
use English;

my ($root) = @ARGV;

if (!defined($root)) {
    die "Usage: $0 root\n";
}

@ARGV = ();

find(
    sub { 
	if (-f && -T) {
	    push @ARGV, $File::Find::name;
	}
    },
    $root
);

$INPLACE_EDIT = '.bak-' . time();

while (<>) {
    s/\015\012?/\012/g;
    print;
}
--- end fix-newlines ---

-- 
Kevin Buettner
kev at primenet.com, kevinb at cygnus.com



More information about the Phoenix-pm mailing list