The first couple versions of `max` read the file into a scalar then back out to the file with two sequential open operations. However with larger files, I figured this could be problematic. I've attempted to correct this below and welcome people's comments on my success or failure. This script attempts to conserve memory through the use of an intermediary temp file and a posix compatible move operation. The host environment on my box is BSD, so you may need to add special directory handling if you're on a different OS.
<CODE> #!/usr/bin/perl -w ############################################################### ### max v0.3a - USAGE: `max [max] file_name` # This application opens an ASCII file and converts the EOL # character to \r\n, \r or \n from any combination of the same. # 'm' == "\r\n" # 'm' stands for MicroSoft # 'a' == "\r" # 'a' stands for Apple # 'x' == "\n" # 'x' stands for Unix/Linux/BSD, etc. ############################################################### use strict; use POSIX; use File::Copy q'mv'; my $rand = '/tmp/'; $rand .= int(rand(99999)); my $usage = "USAGE: `$0 [max] file_name`\n"; die $usage unless ($ARGV[0] && $ARGV[1]); die $usage unless ($ARGV[0] =~ /^[max]$/); die "Can't READ file: $ARGV[1]\n" unless (-r $ARGV[1]); die "Can't WRITE file: $ARGV[1]\n" unless (-w $ARGV[1]); die "Can't WRITE to dir: /tmp\n" unless (-w '/tmp'); die "$ARGV[1] isn't ASCII text\n" unless (-T $ARGV[1]); open TMP, ">$rand.$ARGV[1]"; open IN, "<$ARGV[1]"; while (<IN>) { s/\r\n|\r|\n/\r\n/g if ($ARGV[0] eq 'm'); s/\r\n|\r|\n/\r/g if ($ARGV[0] eq 'a'); s/\r\n|\r|\n/\n/g if ($ARGV[0] eq 'x'); print TMP $_; } close IN; close TMP; mv("$rand.$ARGV[1]","$ARGV[1]"); exit 0; </CODE> FYI, the above code is subject to the attached ascii text file: sfryer.license -- ===================== Shaun Fryer ===================== http://sourcery.ca/ ph: 905-529-0591 =====================
pgp00000.pgp
Description: PGP signature