
xxenclavexx
New User
Nov 8, 2006, 10:11 AM
Post #1 of 4
(293 views)
|
|
Need Help on Script Original progammer is GONE!
|
Can't Post
|
|
Hello, I really don't know too much about perl, but i do know that we have a perl script that cleans up a montly report where I work. I have added on more line to this monthly report, its a receipt number. I have the script here, as you can see it is fairly short, I would appreciate any help I can get. Basically this will go through a text document and if any of the designated fields are missing then this moves it to a new file, what I need to figure out is how to make the new field, that I added to the end of the report, get transfered to the new file.#!c:\perl\bin\perl -wl sub trim($) { my $str = shift(@_); $str =~ s/(^\s+|\s+$)//g; return $str; } die "You must specify an input file mask." if(scalar(@ARGV) != 1); @files = glob($ARGV[0]); foreach $file (@files) { open(READ,$file) or die "Couldn't open input file."; @lines = grep(s/\n//,<READ>); close(READ); $good_fn = $file; $bad_fn = $file; $good_fn =~ s/(\..*$)/\.good$1/; $bad_fn =~ s/(\..*$)/\.bad$1/; if($good_fn eq $file) { $good_fn = ".good.txt"; } if($bad_fn eq $file) { $bad_fn = ".bad.txt"; } open(GOOD,">$good_fn"); open(BAD,">$bad_fn"); foreach $line (@lines) { if(length(trim(substr($line,94,14))) == 0 || length(trim(substr($line,13,11))) == 0) { print BAD $line; next; } # # $lf = substr($line,45,37); $lf =~ s/\./0/g; $line = substr($line,0,45) . $lf . substr($line,82); # print $line; # Here we're filling in the NDC field $lf = trim(substr($line,83,11)); if(length($lf) == 0) { $lf = "99999999999"; $line = substr($line,0,83) . $lf . substr($line,94); } # Fix $lf = substr($line,108,10); if(index($lf,".") != -1) { $lf =~ s/\./0/g; # print "1:",$line; # print "2:",substr($line,0,108),$lf,substr($line,118); $line = substr($line,0,108) . $lf . substr($line,118); } # $lf = trim(substr($line,128,8)); if(length($lf) == 0) { $lf = "00000000"; $line = substr($line,0,128) . $lf . substr($line,136); } # $lf = substr($line,128,10); if(length($lf) == 0) { $lf =~ s/[^A-Za-z0-9]/0/g; $line = substr($line,0,128) . $lf . substr($line,138); } # $lf = trim(substr($line,168,30)); if(length($lf) == 0) { $lf = "12345 "; $line = substr($line,0,168) . $lf . substr($line,198); } # $lf = substr($line,168,30); if($lf =~ /\_/) { $lf =~ s/\_/ /g; $line = substr($line,0,168) . $lf . substr($line,198); } # $lf = trim(substr($line,198,2)); if(length($lf) == 0) { $lf = "CA"; $line = substr($line,0,198) . $lf . substr($line,200); } # $lf = trim(substr($line,200,5)); if(length($lf) == 0) { $lf = "99999"; $line = substr($line,0,200) . $lf . substr($line,205); } print GOOD $line; } close(GOOD); close(BAD); }
|