Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Remove ^M using script
by Corion (Patriarch) on Sep 14, 2007 at 09:55 UTC

    Read the error message.

    perl -pi 's/\r\n/\n/' $a

    is not the same as

    perl -pi -e 's/\r\n/\n/' filename
Re: Remove ^M using script
by eric256 (Parson) on Sep 14, 2007 at 13:51 UTC

    linux already has a command "dos2unix" which fixes the line endings.


    ___________
    Eric Hodges
Re: Remove ^M using script
by riffer (Initiate) on Sep 14, 2007 at 14:38 UTC
    Been using this script for many years to do that. Works great. The critical part is where you do a /\cM/ match and then just chop the end of the line twice.
    #!/usr//bin/perl -w use strict; use Getopt::Long; use IO::File; use constant USAGEMSG => <<USAGE; Usage: clean-ctrl-m.pl [file] Options: --file File to clean (required) --help This Help USAGE my ( @dir, $fh, $tmp_fh, $file, $help, $vers ); $vers = "1.0"; die USAGEMSG unless GetOptions( 'file=s' => \$file, 'help' => \$help ); if ($help) { print "\nVersion: $vers\n"; die USAGEMSG; } if (not $file) { die USAGEMSG; } $fh = new IO::File("$file") or die "File not found: $!\n"; $tmp_fh = IO::File->new_tmpfile; until ($fh->eof()) { my $line = $fh->getline(); if ($line =~ /\cM/) { chop($line); chop($line); } print $tmp_fh "$line\n"; } $fh->close; $fh = new IO::File(">$file") or die "File not found: $!\n"; seek($tmp_fh,0,0); until ($tmp_fh->eof()) { my $line = $tmp_fh->getline(); print $fh $line; } $fh->close; $tmp_fh->close; exit;
Re: Remove ^M using script
by dj_goku (Novice) on Sep 14, 2007 at 20:31 UTC

    Some information about this is at: Newline

    Also if you are using a *nix/BSD OS you might try this at the command line:

    tr -d '\r' < inputfile > outputfile

    I know this really isn't a "script", but just another way to do it. You could just make a system call to tr, and write some extra code to handle switches/parameters.