View Single Post
  #1   (View Single Post)  
Old 30th January 2011
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default Awk script to aggregate MyFreeFarm shopping lists

A file with six shopping list looks like this:
Code:
Shopping List
24x Clover
9x Herbs
54x Cauliflower
96x Tomatoes
54x Spinach
80x Strawberries
6x Honey
Total: 1.463,88 aD
Sell products now?

Shopping List
68x Asparagus
30x Herbs
4x Milk
Total: 982,97 aD
Sell products now?

Shopping List
68x Tomatoes
125x Strawberries
14x Sunflowers
34x Clover
8x Milk
Total: 884,14 aD
Sell products now?

Shopping List
58x Spinach
29x Tubers
65x Radishes
108x Blueberries
101x Onions
7x Wool
Total: 3.186,13 aD
Sell products now?

Shopping List
108x Potatoes
126x Cauliflower
6x Honey
Total: 1.308,18 aD
Sell products now?

Shopping List
60x Potatoes
26x Clover
60x Blueberries
30x Cornflower
90x Raspberries
8x Cheese
Total: 2.775,08 aD
Sell products now?
Feeding this file to the 'shoppinglist.awk' with script produces:
Code:
$ shoppinglist.awk shoppinglist0130  
Radishes        :          65x
Sunflowers      :          14x
Blueberries     :         168x
Spinach         :         112x
Herbs           :          39x
Milk            :          12x
Asparagus       :          68x
Cheese          :           8x
Tubers          :          29x
Cornflower      :          30x
Honey           :          12x
Raspberries     :          90x
Tomatoes        :         164x
Clover          :          84x
Strawberries    :         205x
Wool            :           7x
Cauliflower     :         180x
Onions          :         101x
Potatoes        :         168x
Total           :     10600.38
The script:
Code:
#!/usr/bin/awk -f
# awk program to aggregrate shopping list

/^#/ { next }

/^[0-9]+x/ {
        article[$2] += $1
#       print $2, article[$2]
}

/[Tt]otal/ {
        # convert numbers like 2.000,43 to 2000.43
        sub("[.]", "", $2)
        sub("[,]", ".", $2)
        Amount+= $2
}

END {   
        for (i in article) { 
            #print i, article[i] 
            printf "%15-s : %11dx\n",  i, article[i]
            }
        printf "%15-s : %12.2f\n", "Total", Amount

 }
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote