wapware
asked on
Limit to 3 digit codes.. in the following script.
Hi, Sapa wrote this great script to create .txt files from a list of other files. I need to limit the output files to only 3 letter.txt codes.
Any ideas Sapa?
Regards,
Geoff
#!/usr/bin/perl -w
use strict;
my $indir='/var/www/html/sili con/daily' ;
my $outdir='/var/www/html/sil icon/codes ';
my $maxdate;
if($ARGV[0]) {
$maxdate=($ARGV[0]=~/^[0-3 ]/)?"20$AR GV[0]":"19 $ARGV[0]";
}
my %data;
opendir(D,$indir) or die "opendir: $!";
while(my $fname=readdir(D)) {
$fname=~/^as(\d{6})\.txt$/ i or next;
my $date=$1;
# Convert to 8-digit date and fix Y2K problem
$date=($date=~/^[0-3]/)?"2 0$date":"1 9$date";
next if $maxdate && $maxdate < $date;
open(F,"<$indir/$fname") or next;
while(<F>) {
chomp;
/^(\w+)\s*,(.*)/ or next;
my ($code,$line)=($1,$2);
$data{$code}||=[];
push @{$data{$code}},[$date,$li ne];
}
close(F);
}
foreach my $code (keys %data) {
open(F,">$outdir/$code.txt ") or die "open: $!";
my @sorted=sort { $a->[0] <=> $b->[0] } @{$data{$code}};
foreach (@sorted) {
print F "$_->[1]\n";
}
close(F);
}
Any ideas Sapa?
Regards,
Geoff
#!/usr/bin/perl -w
use strict;
my $indir='/var/www/html/sili
my $outdir='/var/www/html/sil
my $maxdate;
if($ARGV[0]) {
$maxdate=($ARGV[0]=~/^[0-3
}
my %data;
opendir(D,$indir) or die "opendir: $!";
while(my $fname=readdir(D)) {
$fname=~/^as(\d{6})\.txt$/
my $date=$1;
# Convert to 8-digit date and fix Y2K problem
$date=($date=~/^[0-3]/)?"2
next if $maxdate && $maxdate < $date;
open(F,"<$indir/$fname") or next;
while(<F>) {
chomp;
/^(\w+)\s*,(.*)/ or next;
my ($code,$line)=($1,$2);
$data{$code}||=[];
push @{$data{$code}},[$date,$li
}
close(F);
}
foreach my $code (keys %data) {
open(F,">$outdir/$code.txt
my @sorted=sort { $a->[0] <=> $b->[0] } @{$data{$code}};
foreach (@sorted) {
print F "$_->[1]\n";
}
close(F);
}
ASKER
No this script reads files all files in a directory called '/var/www/html/silicon/dai ly' named by date, for example as010613.txt and formated like this,
AAR ,010612,0.020,0.020,0.020, 0.020,2800 0,0
AAT ,010612,0.540,0.540,0.510, 0.520,6033 19,0
AAU ,010612,0.870,0.900,0.870, 0.900,2500 ,0
ABC ,010612,0.530,0.550,0.530, 0.550,1252 16,0
ABCG ,010612,2.015,2.015,2.015, 2.015,4964 ,0
ABG ,010612,2.000,2.040,2.000, 2.000,4597 4,0
To produce many txt files in the directory '/var/www/html/silicon/cod es' of all the codes like
aar.txt
Formated like this;
010402,0.021,0.021,0.021,0 .021,30000 ,0
010403,0.021,0.021,0.020,0 .020,53149 9,0
010404,0.020,0.020,0.020,0 .020,86501 ,0
010405,0.019,0.019,0.019,0 .019,32790 ,0
010406,0.020,0.020,0.020,0 .020,10000 0,0
010411,0.019,0.019,0.019,0 .019,20000 ,0
But I Want to filter to only create txt files of the 3 digit codes.
Regards,
geoff
AAR ,010612,0.020,0.020,0.020,
AAT ,010612,0.540,0.540,0.510,
AAU ,010612,0.870,0.900,0.870,
ABC ,010612,0.530,0.550,0.530,
ABCG ,010612,2.015,2.015,2.015,
ABG ,010612,2.000,2.040,2.000,
To produce many txt files in the directory '/var/www/html/silicon/cod
aar.txt
Formated like this;
010402,0.021,0.021,0.021,0
010403,0.021,0.021,0.020,0
010404,0.020,0.020,0.020,0
010405,0.019,0.019,0.019,0
010406,0.020,0.020,0.020,0
010411,0.019,0.019,0.019,0
But I Want to filter to only create txt files of the 3 digit codes.
Regards,
geoff
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works like a charm.
Thanks
Geoff
Thanks
Geoff
"this.text" ?
it doesn't appear that that is possible.