DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #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
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple entries in job lists ros2468 OpenBSD General 1 19th February 2010 07:45 AM
Backup script(s)? giddyupman General software and network 2 3rd January 2009 02:06 PM
troff/nroff Q; how to put a linebreak in a tag labeled lists item TerryP General software and network 8 10th November 2008 07:39 AM
my 1st sh script graudeejs Programming 12 18th August 2008 10:25 PM
Router shopping Yuka General Hardware 8 23rd July 2008 02:51 AM


All times are GMT. The time now is 08:26 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick