I get the following results when running the perl script below. For
some reason I get (3) lines that don't match the regex criteria before
it reaches the first line that has data, and one additional line after
the script completes. Is there a way to not write the line if the
varaiable $cur_sym is empty?
Thanks in advance,
Jack
Output from script:
<-------->
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, "currency") || 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+)/;
# Strip the trailing spaces from $cur_desc
StripTSpace($cur_desc);
$cur_sym and $started++;
printf OUTFILE "%s\,%s\,%s\,%s\,%s\,%s\,%s\n",
$date, $time, $tz, $cur_sym, $cur_desc, $usd_unit, $units_usd;
not $cur_sym and ($started and last) or next;
$started or print STDERR "Didn't find a currency line";
}
close(INFILE);
close(OUTFILE);
print STDERR "\n";
1;