CRLF

Kevin Buettner plug-discuss@lists.PLUG.phoenix.az.us
Fri, 19 Oct 2001 12:21:31 -0700


On Oct 19, 11:07am, Lucas Vogel wrote:

> Does anyone know of a utility that will go through files and replace the
> CR/LF characters with the unix variant(isn't it just LF?)?

The program appended below does what you want on an entire directory
hierarchy.  I wrote it back in the days when I frequently needed to
handle Mac text files.  It'll work on those files too.  It even creates
timestamped backup files.

For those of you who're wondering why I wrote:

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

instead of the (nicer looking):

    s/\r\n?/\n/g;

It's because the former is more portable.  The meaning of \r and \n
changes depending upon the platform.

--- 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 ---