Solved

Limit to 3 digit codes.. in the following script.

Posted on 2001-06-12
4
167 Views
Last Modified: 2010-03-05
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/silicon/daily';
my $outdir='/var/www/html/silicon/codes';
my $maxdate;
       
if($ARGV[0]) {
   $maxdate=($ARGV[0]=~/^[0-3]/)?"20$ARGV[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]/)?"20$date":"19$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,$line];
   }
   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);
}
       
0
Comment
Question by:wapware
  • 2
4 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 6183811
are you saying it is possible to create a file called

"this.text" ?

it doesn't appear that that is possible.
0
 

Author Comment

by:wapware
ID: 6183833
No this script reads files all files in a directory called '/var/www/html/silicon/daily' named by date, for example as010613.txt and formated like this,

AAR   ,010612,0.020,0.020,0.020,0.020,28000,0
AAT   ,010612,0.540,0.540,0.510,0.520,603319,0
AAU   ,010612,0.870,0.900,0.870,0.900,2500,0
ABC   ,010612,0.530,0.550,0.530,0.550,125216,0
ABCG  ,010612,2.015,2.015,2.015,2.015,4964,0
ABG   ,010612,2.000,2.040,2.000,2.000,45974,0

To produce many txt files in the directory '/var/www/html/silicon/codes' 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,531499,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,100000,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

0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 6183953
/^(\w{3})\s*,(.*)/ or next;
0
 

Author Comment

by:wapware
ID: 6183978
Works like a charm.

Thanks

Geoff
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
remove duplicates from the csv file 13 95
Regex rule to match two different url 5 36
combine multiple lines 2 21
Perl tar error 8 30
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now