aboutsummaryrefslogtreecommitdiffstats
path: root/ledgerscripts/apply_categories.pl
blob: 94bb913e2e2de76b26a3eae9210dc63f5e3299c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env perl

use warnings;
use strict;


open (my $cat_file, "<", "expense_categories") or die 
    "Can't open expense_categories file";

my %categories;

while (local $_ = <$cat_file>) {
    if (/(.*)@(.*)/) {
        $categories{$1} = $2;
    }
}
close $cat_file;

local $/ = ""; # switch to paragraph mode

while (my $block = <>) {
    if ($block =~ m/\d{4}.*\* (.*)\n.*(Expenses:.*)£/s) {
        while (my ($k, $v) = each %categories) {
            if ($k eq $1) {
                $block =~ s/$2/Expenses:$v/;
            };     
        }
        print $block;
    };
}