Here's the results that I get with the regex below. I don't know why
I'm getting the first three lines or why it stops after the seventh of
apx90 lines.
I'd appreciate any help in fixing this.
Thanks,
Jack
2000-12-29,16:16:19,PST,,,,
2000-12-29,16:16:19,PST,,,,
2000-12-29,16:16:19,PST,,,,
2000-12-29,16:16:19,PST,USD,United States Dollars ,1.00000,1.00000
2000-12-29,16:16:19,PST,EUR,Euro
,0.941604,1.06202
2000-12-29,16:16:19,PST,GBP,United Kingdom Pounds
,1.49242,0.670052
2000-12-29,16:16:19,PST,CAD,Canada Dollars
,0.666790,1.49972
2000-12-29,16:16:19,PST,DEM,Germany Deutsche Marks
,0.481435,2.07712
2000-12-29,16:16:19,PST,FRF,France Francs
,0.143547,6.96638
2000-12-29,16:16:19,PST,JPY,Japan Yen
,0.00872554,114.606
2000-12-29,16:16:19,PST,,,,
#!/usr/bin/perl
#
# cur2csv.pl
#
use strict;
use vars qw($started);
use vars qw($cur_sym $cur_desc $usd_unit $units_usd);
use vars qw($year $month $mday $hour $minute $second $timezone);
use vars qw($conv_date $date $time $tz);
use Date::Manip;
use String::Strip;
use DBI;
use DBD::Pg;
open (OUTFILE, ">", "currency.csv") || die "Can not open currency.csv
for writing";
printf STDERR "Reading currency file...";
open (INFILE, "curtest") || die "Can not open /var/spool/mail/currency
for reading";
while (<INFILE>) {
# Extract date and time of Currency Rate Quotation
($year, $month, $mday, $hour, $minute, $second, $timezone) =
/^Rates as of (\d+).(\d+).(\d+) (\d+):(\d+):(\d+) (\w+) (.*)$/;
# Convert date from UTC (GMT) to PST8PDT and adjust date and time
accordingly.
$tz = &Date_TimeZone;
$conv_date = "$year-$month-$mday $hour:$minute:$second";
$conv_date = &ParseDate($conv_date);
$conv_date = &Date_ConvTZ($conv_date, $timezone, $tz);
$date = &UnixDate($conv_date,"%Y-%m-%d");
$time = &UnixDate($conv_date,"%H:%M:%S");
$tz = &UnixDate($conv_date,"%Z");
$year and last; # If we've matched the data line, then bail out.
eof and print STDERR "Didn't find the date line";
}
# Extract the ISO 4217 Code for Currencies and Funds (1995)
# Extract the Currency Description, and trim the trailing spaces
# Extract US Dollars to Units rate, and trim the leading/trailing
spaces
# Extract Units to US Dollars rate, and trim the leading/trailing
spaces
while (<INFILE>) {
($cur_sym, $cur_desc, $usd_unit, $units_usd) =
/^([A-Z]{3})\s+([A-Za-z\s]{28})\s+(\d+\.\d+)\s+(\d+\.\d+)/;
printf OUTFILE "%s\,%s\,%s\,%s\,%s\,%s\,%s\n",
$date, $time, $tz, $cur_sym, $cur_desc, $usd_unit, $units_usd;
#print OUTFILE $date, $time, $tz, $cur_sym, $cur_desc, $usd_unit,
$units_usd;
$cur_sym and $started++;
# $started or print STDERR "Didn't find a currency line";
not $cur_sym and ($started and last) or next;
$started or print STDERR "Didn't find a currency line";
# substr($cur_desc, 0, 1) = ''; # Delete leading space
}
close(INFILE);
close(OUTFILE);
print STDERR "\n";
1;