Awk Script that implements a report writer

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Awk Script that implements a report writer
# 1  
Old 11-22-2010
Awk Script that implements a report writer

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Write an awk script that implements a report writer.
Here is what I need to do:
The report computes summary data on the [COLOR=blue ! important][COLOR=blue ! important]performance[/COLOR][/COLOR] of sales associates for our company "Lazy Acres, Inc.".
The awk script file name must be "report".
The awk script is invoked from the command line with an inputfile that contains data on products, associates and [COLOR=blue ! important][COLOR=blue ! important]sales[/COLOR][/COLOR]:
awk -f report inputfile
The lines in the input file use ":" as field separator and are of any of these 3 kinds:
lines that describe products have the following fields:
* product id: an integer number
* description: alphanumeric text
* price: floating point number, with 2 significant digits
lines that describe associates have the following fields:
* associate id: an integer number
* first name: alphanumeric text
* last name: alphanumeric text
* salary: an integer number
* position: alphanumeric text
lines that describe sales have the following fields:
* product id: an integer number
* quantity: an integer number
* date: in the form of mm/dd/yyyy
* associate id: an integer number
( click here for a sample inputfile )
The script should compute the sale amounts per associate for the year 2009 and print them in a sorted list ranked according to the sales amount.
Here is an example of the script invocation using the data provided in the example file:
$ awk -f report inputfile
Lazy Acres, Inc.
2009 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 Commedian 9.90
$

2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):
This is what I have so far, I don't know what to do next:

Code:
Code:
  BEGIN{print "Lazy Acres, Inc"
      print "2009 Sales associates Ranking"
      print "Name        Position        Sales amount"
      print "========================================"
      FS=":"
      }

NF==3 {cost[$1]=$3}


NF==4 && $3 ~ /2009/ {tot = $2 * cost[$1]
                      emplId[$4] +=tot}

NF==5 {  emplId2[$1]
         first[$2]
         last[$3]
         position[$5]

      }

I think i need to get emplId to represent the person and position but I don't know how to do that, any help leading me in the right direction would be great, I've attatched the inpute file to view



4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
niu, dekalb, Il, ege, 330

Last edited by bravens52; 11-22-2010 at 08:24 PM.. Reason: code tags, please...
# 2  
Old 11-24-2010
Hi,

I get confused because your explanation of fields doesn't match with the input field provided.

Product lines have 4 fields, associate lines have only 3 fields, and the third one is the position, not the 'last name' (¿?). The sales lines have 5 fields, and I think the date is fourth, the third one is the quantity and the second one the product id.

Correct me if i am wrong but seems confused for me. Even so I have made an attemp, test it if can be useful for you:

Code:
$ awk --version | sed '1q'
GNU Awk 3.1.8
$ cat report
BEGIN {
    ## Field separator.
    FS=":";

    ## Header.
    printf("%s\n", "Lazy Acres, Inc.");
    printf("%s\n", "2009 Sales Associates Ranking");
    printf("%-30s %-20s %20s\n", "Name", "Position", "Sales amount");
    for (i = 0; i < 72; i++) {
    printf("%c", "=");
    }
    print;
}

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

## Product lines.
## $1 -> Product ID.
## $4 -> Price.
NF == 4 {
    product[$1] = $4;
}

## Sales lines.
## $2 -> Product ID.
## $3 -> Quantity.
## $4 -> Date.
NF == 5 && $4 ~ /2009/ {
    if ($2 in product) { sales[assoc[$NF]] += $3 * product[$2]; }
}

END {
    ## Sorted associates names.
    asorti(sales, salesSortedI);
    ## Reverse sorted sales (numeric numbers from smallest to biggest).
    asort(sales, salesSortedD);

    j = 0;
    for (i = length(sales); i > 0; i--) {
    j++;
    match(salesSortedI[j], /(.*):(.*)/, a);
    printf("%-30s %-20s %20s\n", a[1], a[2], salesSortedD[i]);
    }
}
$ awk -f report inputfile
Lazy Acres, Inc.
2009 Sales Associates Ranking
Name                           Position                     Sales amount
========================================================================
Buck, Fast                     Stock Boy                         2398.89
Doe, John                      Clerk                             1059.67
Lindon, Rosemary               Producer                           139.76
Miller, Dennis                 Comedian                           134.01
Rush, George                   Salesman                               31
Worker, Susan                  Manager                              8.91

Regards,
Birei
# 3  
Old 11-24-2010
okay yeah i definitely made mistakes. I will take what you have and do some trial in error myself..thanks for the help..i really appreciated it
# 4  
Old 12-01-2010
Hi,

Output was incorrect. Here a new version:
Code:
$ awk --version | sed '1q'
GNU Awk 3.1.8
$ cat report
BEGIN {
    ## Field separator.
    FS=":";

    ## Header.
    printf("%s\n", "Lazy Acres, Inc.");
    printf("%s\n", "2009 Sales Associates Ranking");
    printf("%-30s %-20s %20s\n", "Name", "Position", "Sales amount");
    for (i = 0; i < 72; i++) {
    printf("%c", "=");
    }
    print;
}

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

## Product lines.
## $1 -> Product ID.
## $4 -> Price.
NF == 4 {
    product[$1] = $4;
}

## Sales lines.
## $2 -> Product ID.
## $3 -> Quantity.
## $4 -> Date.
NF == 5 && $4 ~ /2009/ {
    if ($2 in product) { sales[assoc[$NF]] += $3 * product[$2]; }
}

END {
    ## Reverse sorted sales (numeric numbers from smallest to biggest).
    asort(sales, salesSortedD);

    for (i = length(sales); i > 0; i--) {
    for (s in sales) {
        if (sales[s] == salesSortedD[i]) {
        match(s, /(.*):(.*)/, a);
        printf("%-30s %-20s %20.2f\n", a[1], a[2], salesSortedD[i]);
        break;
        }
    }
    
    }
}
$ awk -f report inputfile
$ ## Output supressed.

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 5  
Old 12-01-2010
can you show how to do END with the sales sorted in printf as: printf(....) | "sort...."
# 6  
Old 12-01-2010
Hi,

Could be something like this? I had to change spaces to tabs and output should be formatted better, but I hope you get the idea.
Code:
$ tail -n6 report        ## Part of the script supressed.
END {
    for (s in sales) {
    match(s, /(.*):(.*)/, a);
    printf("%-30s\t%-20s\t%20.2f\n", a[1], a[2], sales[s]) | "sort -t\"\t\" -nr -k3"
    } 
}
$ awk -f report inputfile
Lazy Acres, Inc.
2009 Sales Associates Ranking
Name                           Position                     Sales amount
========================================================================
Buck, Fast                      Stock Boy                            2398.89
Rush, George                    Salesman                             1059.67
Worker, Susan                   Manager                               139.76
Doe, John                       Clerk                                 134.01
Lindon, Rosemary                Producer                               31.00
Miller, Dennis                  Comedian                                8.91

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 7  
Old 12-01-2010
thanks, why do the spaces have to be changed to tabs?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to implements Queueing Using Shell scripts

I want to implement a control mechanism using Shell scripts .The intention is to have controlled number of jobs running in parallel External process will kickstart 40 jobs in parallel .All the 40 jobs will call the same generic script with different parameter values .But at a time only 2 should... (1 Reply)
Discussion started by: police
1 Replies

2. Homework & Coursework Questions

awk script that implements a report writer

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The script should compute the sale amounts per associate for the year 2009 and print them in a sorted list ranked... (1 Reply)
Discussion started by: Jeffthrow5
1 Replies

3. HP-UX

How Unix implements virtual memory?

Hello! just wanna ask if how UNIX implements virtual memory, and how it handles page faults, working sets, page sizes and how it reconciles thrashing issues? if you know some sources where I can have some idea, just post it here. thx (1 Reply)
Discussion started by: kjcruz
1 Replies

4. Homework & Coursework Questions

How Unix implements virtual memory?

Use and complete the template provided. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The key to using memory most efficiently is virtual memory management. Consider both Windows and UNIX operating systems. Compare and contrast how each... (0 Replies)
Discussion started by: kjcruz
0 Replies

5. Shell Programming and Scripting

Awk Script for generating a report

Hi all, I have a log file of the below format. 20081016:000042 asdflasjdf asljfljs asdflasjf safjl 20081016:000229 /lask/ajlsdf/askdfjsa 20081016:000229 /lashflas /askdfaslj hsfhsahf 20081016:000304 lasflasj ashfashd 20081016:000304 lajfasdf ashfashdfhs I need to generate a... (3 Replies)
Discussion started by: manoj.naidu
3 Replies

6. AIX

Probleme with DVD Writer

I can write into DVD? I have USE "MKCD" command mkcd -r directorie -d /dev/cd1 please help me it s urgent (2 Replies)
Discussion started by: mktahar
2 Replies

7. UNIX for Advanced & Expert Users

Probleme With DVD Writer

I Use mkcd for save same directories into DVD, But the commande not complete succefuly mkcd -r directorie_i_whish_save -d /dev/cd1 please it is very urgent thank you :confused: :confused: (1 Reply)
Discussion started by: mktahar
1 Replies

8. UNIX for Dummies Questions & Answers

Share CD-Writer

Hi, guys ! I have a server that runs FreeBSD 5.3 and on that server a have a CD-Writer used for backup. The question is, does anybody know how can I share this CD-Writer ? I want to be able to write CDs from another computer, without transfering all the data to the server and after that use all... (2 Replies)
Discussion started by: Sergiu-IT
2 Replies
Login or Register to Ask a Question