Category: linux administration
Author/Contact Info Jerry Fowler - jerry at digilliance.net
Description: A very useful block of code for web server administrators. Converts DOS format files uploaded in BINARY mode to UNIX format by stripping the Control M characters.

Usage: Copy to /usr/bin/stripline, chmod to 755, then you (or any user) can:

stripline in.file out.file
to fix a mis-uploaded file.

Disclaimer: This could be considered reinventing the wheel as there are code snippets around that do the same function. This one is however set up especially for use as a system utility.

#!/usr/bin/perl

use strict;

if ($ARGV[0] eq undef) {
 print "stripline (GNU GPL)\nSept 15, 2001 Jerry D. Fowler jerry\@digi
+lliance.net\n";
 print "Usage: stripline in.file out.file\n\n";
 exit;
}

chdir($ENV{PWD}) or die "Could not change to your current directory\n"
+;
open (FILE, "$ARGV[0]") or die "Could not create Outfile";
open (OUT, ">$ARGV[1]") or die "Could not create Outfile\n";
while (<FILE>) {
$_ =~ s/\cM//g;
chomp;
print OUT "$_\n";
}
close FILE;
close OUT;
Replies are listed 'Best First'.
Re: stripline
by rob_au (Abbot) on Sep 16, 2001 at 07:23 UTC
    The following one-liner will edit the file in place and make a backup called in.file.bak. Also too, shell expansion can be used to process multiple files at once.

    perl -pi.bak -e 's/\cM//g' in.file

     

    Ooohhh, Rob no beer function well without!

Re: stripline
by Rex(Wrecks) (Curate) on Sep 24, 2001 at 21:02 UTC
    Most varieties of *nix come with or have an installable port called dos2unix that does this very job. Works great and is less overhead than loading perl to do it.

    Then again, TMTOWTDI

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!