modify if necessary: supermarket identifiers (@markets)
#!/usr/bin/perl
## extracts and displays those amounts from your bank's transactions csv file
## which have gone to supermarkets and calculates a total sum (for some timeperiod, for example)
## expects beneficiary column before amount column
## Text::CSV::Simple expects commas for separators, you may have to tweak your csv format; otherwise you'll see a lot of "Failed on" output
## usage:
## perl $0 [csvfile] [beneficiary_col] [amount_col]
## e.g.
## perl supermarkets.pl umsatz.csv 6 9
use strict;
use warnings;
#use Data::Dumper; # for debugging hashrefs
use Text::CSV::Simple;
# install with: perl -MCPAN -e "install Text::CSV::Simple"
my @markets = qw(KAISERS E-REICHELT REWE LIDL);
my $markets = join('|', @markets); # for regex matching
my $csvfile = shift;
my $beneficiary_col = shift; # column no. where to expect (among others) the markets' names
my $amount_col = shift; # column no. where to expect the amount
my ($amount_total, $match) = (0) x 2;
my $parser = Text::CSV::Simple->new;
# Text::CSV::Simple's column count starts with 0 so we have to correct the user input field numbers
$parser->want_fields($beneficiary_col-1, $amount_col-1);
# caution: field naming sorts somewhat(?) alphabetically
$parser->field_map(qw/1 2/);
# gets array of hashes
my @lines = $parser->read_file($csvfile);
# debugging hashrefs (where's all the expected data?)
#print Dumper(\@lines);
# print csv-style headers
print "Supermarkt;Betrag;Summe\n";
foreach my $line (@lines) {
foreach my $column (keys %{$line}) {
if (${$line}{$column} =~ /$markets/) {
print ${$line}{$column};
$match = 1; # found a market string in this field
}
# we have found a market,
# so now we're interested in the next field
elsif ($match == 1) {
print ";${$line}{$column}"; # amount
${$line}{$column} =~ s/,/\./; # swap ',' with '.' in amount so we can calc
$amount_total += ${$line}{$column};
print ";$amount_total\n";
$match = 0; # reset
}
}
}
Keine Kommentare:
Kommentar veröffentlichen