Sponsored Content
Homework and Emergencies Homework & Coursework Questions Awk Script that implements a report writer Post 302474597 by birei on Wednesday 24th of November 2010 06:11:24 PM
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
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
CUBRID_FETCH_FIELD(3)							 1						     CUBRID_FETCH_FIELD(3)

cubrid_fetch_field - Get column information from a result and return as an object

SYNOPSIS
object cubrid_fetch_field (resource $result, [int $field_offset]) DESCRIPTION
This function returns an object with certain properties of the specific column. The properties of the object are: o $name -column name o $table -name of the table that the column belongs to o $def -default value of the column o $max_length -maximum length of the column o $not_null -1 if the column cannot be NULL o $primary_key -1 if the column is a primary key o $unique_key -1 if the column is an unique key o $multiple_key -1 if the column is a non-unique key o $numeric -1 if the column is numeric o $blob -1 if the column is a BLOB o $type -the type of the column o $unsigned -1 if the column is unsigned o $zerofill -1 if the column is zero-filled PARAMETERS
o $result -$result comes from a call to cubrid_execute(3) o $field_offset - The numerical field offset. If the field offset is not specified, the next field (that was not yet retrieved by this function) is retrieved. The $field_offset starts at 0. RETURN VALUES
Object with certain properties of the specific column, when process is successful. FALSE on failure. EXAMPLES
Example #1 cubrid_fetch_field(3) example <?php $conn = cubrid_connect("localhost", 33000, "demodb"); $req = cubrid_execute($conn, "SELECT event_code,athlete_code,nation_code,game_date FROM game WHERE host_year=1988 and event_code=20001;"); var_dump(cubrid_fetch_row($req)); cubrid_field_seek($req, 1); $field = cubrid_fetch_field($req); printf(" --- Field Properties --- "); printf("%-30s %s ", "name:", $field->name); printf("%-30s %s ", "table:", $field->table); printf("%-30s "%s" ", "default value:", $field->def); printf("%-30s %d ", "max length:", $field->max_length); printf("%-30s %d ", "not null:", $field->not_null); printf("%-30s %d ", "primary key:", $field->primary_key); printf("%-30s %d ", "unique key:", $field->unique_key); printf("%-30s %d ", "multiple key:", $field->multiple_key); printf("%-30s %d ", "numeric:", $field->numeric); printf("%-30s %d ", "blob:", $field->blob); printf("%-30s %s ", "type:", $field->type); printf("%-30s %d ", "unsigned:", $field->unsigned); printf("%-30s %d ", "zerofill:", $field->zerofill); cubrid_close_request($req); cubrid_disconnect($conn); ?> The above example will output: array(4) { [0]=> string(5) "20001" [1]=> string(5) "16681" [2]=> string(3) "KOR" [3]=> string(9) "1988-9-30" } --- Field Properties --- name: athlete_code table: game default value: "" max length: 0 not null: 1 primary key: 1 unique key: 1 multiple key: 0 numeric: 1 blob: 0 type: integer unsigned: 0 zerofill: 0 PHP Documentation Group CUBRID_FETCH_FIELD(3)
All times are GMT -4. The time now is 01:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy