Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: unix script programming using awk  (Read 3834 times)

0 Members and 1 Guest are viewing this topic.

ajwest

  • Guest
unix script programming using awk
« on: May 03, 2011, 09:15:33 AM »
The awk script is invoked from the command line with an inputfile that contains data on products, associates and sales

My problem is printing the name array and the sales array at the same time. so far i got my program problem to print the same value in the name's array over and over again and the sales array work just fine. Also i want to sort the output from highest to lowest base on the sale's array

here's my code so far

Code: [Select]
# !/bin/bash

# Adrien Jeffries
# Assingtment 8
# due 05-02-11
# Write an awk script that produces
# a report from an input file

BEGIN {
# printing header
print "Lazy Acres, Inc."
print "2010 Sales Associates Ranking"
print "Name          Position        Sales Amount"
print "=========================================="
# setting field separator to :
FS = ":"
}

# Product lines
# $1 = Product ID.
# $2 = Product Caregory
# $3 = Description
# $4 = Price
NF == 4 {
    product[$1] = $4;
}

# Associates lines
# $1 = Associate ID.
# $2 = Name
# $3 = Position.
NF == 3 {
    name[$1] = $2  ":" $3;
}

# Sales lines.
# $1 = Transaction ID
# $2 = Product ID
# $3 = Quantity
# $4 = Date
# $5 = Assoiciate ID
NF == 5 && $4 ~ /2010/ {
    if ($2 in product) { sales[$5] += $3 * product[$2]  }
}
END {
    # Print out the report in descending order base on sale
     asort(sales, salesSorted);

    for (i = length(sales); i > 0; i--)
     {
       for (x in sales)
         {
          if (sales[x] == salesSorted[i])
          {
            for( y in name)
             {
              printf("%20s %18.2f\n", name[y], salesSorted[i]);
              break;
              }
           }
        }
    }
}

here the inpute file that im using
Code: [Select]
108:Maritime Accessories:wheel:49.99
109:Boating Part:transom:199.00
102:Boating Part:trailer hitch:97.95
103:Maritime Accessories:sway bar:49.99
110:Tackle:pulley:9.88
101:Boating Part:propeller:104.99
105:Tackle:mirror:4.99
111:Maritime Accessories:lock:31.00
104:Tackle:fishing line:0.99
113:Tackle:fish bait:1.00
106:Tackle:cup holder:2.49
107:Maritime Accessories:cooler:14.89
112:Boating Part:boat cover:120.00

26:Miller, Dennis:Comedian
25:Lindon, Rosemary:Producer
23:Worker, Susan:Manager
21:Doe, John:Clerk
24:Buck, Fast:Stock Boy
22:Rush, George:Salesman

237787:113:92:12/22/2008:24
291065:104:9:05/06/2010:26
845813:104:9:05/06/2009:26
152208:104:9:05/03/2008:21
585537:104:9:03/03/2008:21
193475:104:9:03/03/2008:21
810622:107:9:03/01/2010:21
173530:107:9:03/19/2009:21
484983:104:9:03/01/2008:21
151862:104:9:03/01/2008:21
366957:111:4:12/02/2008:21
491736:111:4:12/02/2008:21
910094:104:3:03/02/2009:24
265256:108:21:03/02/2010:22
893187:108:21:03/02/2009:22
220350:104:2:05/03/2008:24
631798:104:2:05/03/2008:24
571077:104:2:03/03/2010:24
332157:104:2:03/03/2010:24
104454:104:2:03/03/2010:24
# example of erroneous line
502930:104:2:03/03/2009:24
567394:104:2:03/02/2010:24
854164:104:2:03/02/2010:24
040140:104:2:03/02/2010:24
108022:104:2:03/02/2010:24
639395:106:2:03/02/2010:24
013111:106:2:03/02/2010:24
720184:104:2:03/02/2009:24
101814:104:2:03/02/2009:24
482945:104:2:03/02/2009:24
254355:105:2:03/01/2010:24
676697:105:2:03/01/2010:24
896782:110:2:03/01/2008:23
759895:110:2:03/01/2008:23
720292:110:2:02/02/2009:23
907622:109:12:03/01/2010:24
928610:109:12:03/01/2009:24
377662:104:1:05/03/2010:26
899755:112:1:05/23/2008:23
778879:109:1:03/03/2010:24
324254:104:1:03/03/2008:26
194231:104:1:03/03/2008:26
995552:104:1:03/02/2008:26
161296:104:1:03/02/2008:26

so my output to look like this


% awk -f z123456.rank inputfile
 Lazy Acres, Inc.2010
Sales Associates Ranking
Name                       Position               Sales Amount
==========================================
Buck, Fast               Stock Boy               2630.78
Rush, George          Salesman               1049.79
Worker, Susan        Manager                   360.00
Doe, John                Clerk                         134.01
Lindon, Rosemary    Producer                     31.00
Miller, Dennis           Comedian                     9.90