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