http://qs321.pair.com?node_id=8991
Category: data formatting
Author/Contact Info T.R. Fullhart, kayos@kayos.org
Description:

This converts the line-endings of a text file (with unknown line-endings). It supports DOS-type, Unix-type, and Mac-type. It converts the files "in place", so be careful.

You call it like:

linendings --unix file1.txt file2.txt ...
#!/usr/bin/perl

my $lineending = "\n";

my $type = shift @ARGV;

if( $type =~ /unix/ ) {
        $lineending = "\012";
} elsif( $type =~ /dos/ ) {
        $lineending = "\015\012";
} elsif( $type =~ /mac/ ) {
        $lineending = "\015";
} else {
        print "Usage: $0 --unix|--dos|--mac\n";
        exit 1;
}

my @files = @ARGV;

for my $file ( @files ) {
        open FILE, $file or next;        # thanks turnstep
        my @lines = <FILE>;
        close FILE;

        foreach my $i ( 0..$#lines ) {
                $lines[$i] =~ s/(\012|\015\012?)/$lineending/g;
        }

        open FILE,">$file";
        print FILE @lines;
        close FILE;
}