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;

In reply to Re: Remove ^M using script by riffer
in thread Remove ^M using script by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.