Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

unsortedNames.txt: N D Street Faye King Lenny R Graph I M Board Mark R Board Sal

ID: 3871875 • Letter: U

Question

unsortedNames.txt:

N D Street
Faye King
Lenny R Graph
I M Board
Mark R Board
Sal Ed Barr
Grey Wire
Holly Graph
Perry Scope
Bob L Head
Joe King
Holly Wood
Ava Kashun
Jean E Us
Mac A Ronee
E Racer
Hammond Ecks
B B Gunn
Cooke King
Tuck King
N D Carr
Otto Graph
Dee Boat
Anita Goodgrade

reservation.txt:

CUST M 1986/01/01 petem@xyz.net Pete Moss
RESERVE H100.15005 2 110.00
RESERVE H222.15005 2 170.00
RESERVE H200.15010 2 75.00
RESERVE H333.15010 2 200.00
ENDCUST
CUST M 1957/02/02 pcorn@abc.net Pop Corn
RESERVE H222.15005 10 170.00
RESERVE H333.15010 5 200.00
ENDCUST
CUST F 1958/03/03 pennyl@xyz.net Penny Loafer
RESERVE H444.15001 1 125.00
RESERVE H333.15010 1 200.00
ENDCUST
CUST M 1979/04/04 hgilmore@xyz.net Adam Sandler
RESERVE H100.15005 3 110.00
RESERVE H222.15005 3 170.00
RESERVE H333.15010 3 200.00
ENDCUST
CUST F 1989/05/05 butter@xyz.net Melba Toast
RESERVE H333.15010 2 200.00
RESERVE H444.15015 2 125.00
ENDCUST

Part A. You have been provided an unsorted file of names (/usr/local/courses/clark/cs3423/2017Fa/Proj3/unsortedNames.txt). Each name ends with a last name and may have multiple names and/or initials before the last name (unfortunately it varies). Use awk and sort to create a file sorted by last name. This may involve multiple awk and one sort step. The resulting sorted file should look similar to the original file except that it is sorted by last name Hint: Since the number of words in a name varies, we need to create an intermediate that has the last name first to simplify sorting. After sorting, we no longer want that last name as the first value. Create a bash file named p3a.bash to create the sorted file. Besides the shebang line, there should be only one other line in the bash file. That line must use pipes between the steps Part B. You have been provided a customer reservation transactions file (/usr/local/courses/clark/cs3423/2017Fa/Proj3/reservation.txt). It contains three types of records: a customer definition record containing gender, birthdate, email address, and full name a reservation request containing flight number, number of seats requested, and cost per seat. These reservations are for the customer defined by the most recent CUST transaction CUST RESERVE ENDCUST a sentinel marking the end of the transactions for one customer. Example data for one customer: CUST M 1957/02/02 pcorn@abc.net Pop Corn RESERVE H222.15005 10 170.00 RESERVE H333.15010 5 200.00 ENDCUS For part B, you must produce the following output 1. Print a table heading showing "Name", "Flight", "Seats", and "Total Cost" 2. For each customer's reservations, show last name(max of 12 characters), flight number (max of 10 characters, number of seats requested, and total cost (number of seats requested cost per seat) 3. After processing the customer's reservations, show "Total" (in the Seats column) and his/her total cost under the Total Cost column 4. After processing all the data Print a table heading that shows "Flight", "Total Seats" For each flight, show flight number and the total of all seats requested across all customers Sample output: Name Moss Flight H100.15005 SeatsTotal Cost 220.00 2

Explanation / Answer

p3a.awk
--------------------------------------------------------
# gawk -f p3a.awk unsortedNames.txt
BEGIN {}
{
    # Sort line last name -> first name
    for(i=NF; i > 0; i--)
    {
       printf("%s ", $i);
    }
     printf(" ");
}
END {}
-------------------------------------------------------
p3a.bash
----------------------------------
#!/bin/bash

# Use gawk script to print last names first
# Pipe into sort to sort by last names
# Reverse the sorted strings to show first -> last name

# gawk -f p3a.awk unsortedNames.txt | sort
gawk -f p3a.awk unsortedNames.txt | sort | gawk -f p3a.awk > p3a.out
---------------------------------------------------------
p3a.out
-----------------------------------------------
Sal Ed Barr
I M Board
Mark R Board
Dee Boat
N D Carr
Hammond Ecks
Anita Goodgrade
Holly Graph
Otto Graph
Lenny R Graph
B B Gunn
Bob L Head
Ava Kashun
Cooke King
Faye King
Joe King
Tuck King
E Racer
Mac A Ronee
Perry Scope
N D Street
Jean E Us
Grey Wire
Holly Wood
--------------------------------------------------
p3b.awk
-----------------------------
# gawk -f p3b.awk reservation.txt
BEGIN {
    printf("%-12s %-10s %5s %10s ", "Name", "Flight", "Seats", "Total Cost");
    Name = "";
    CustCost;
}
{
    # CUST gives new customer info
    # Reset Customer Cost
    if($1 == "CUST")
    {
        Name = $NF;
        CustCost = 0;
    }
    # RESERVE gives flight information
    # $2 = Flight, $3 = Seats, $4 = Total Cost
    # Flight is associative array holding values of seats per flight
    else if($1 == "RESERVE")
    {
        printf("%-12s %10s %5s %10s ", Name, $2, $3, $4);
        CustCost += $3*$4;
        Flight[$2] += $3;
    }
    # ENDCUST ends customer info.
    # Print Total Customer cost
    else if($1 == "ENDCUST")
    {
        printf("%30s %9.2lf ", "Total:", CustCost);
    }
    # Else invalid input was received
    else{
        printf("Invalid Input ");
    }
}
END {
    # Print the Flights and Number of seats per flight
    printf("%-10s %10s ", "Flight", "Total Seats");
    for(key in Flight)
    {
        printf("%-10s %10s ", key, Flight[key]);
    }  
}
----------------------------------------------------------------------------
p3b.bash
--------------------------------------------
#!/bin/bash

# Print Flight information using awk script
gawk -f p3b.awk reservation.txt > p3b.out
---------------------------------------------------------------------------
p3b.out
-------------------------------------------
Name         Flight     Seats Total Cost
Moss         H100.15005     2     110.00
Moss         H222.15005     2     170.00
Moss         H200.15010     2      75.00
Moss         H333.15010     2     200.00
                        Total:   1110.00
Corn         H222.15005    10     170.00
Corn         H333.15010     5     200.00
                        Total:   2700.00
Loafer       H444.15001     1     125.00
Loafer       H333.15010     1     200.00
                        Total:    325.00
Sandler      H100.15005     3     110.00
Sandler      H222.15005     3     170.00
Sandler      H333.15010     3     200.00
                        Total:   1440.00
Toast        H333.15010     2     200.00
Toast        H444.15015     2     125.00
                        Total:    650.00
Flight     Total Seats
H333.15010         13
H444.15001          1
H200.15010          2
H444.15015          2
H222.15005         15
H100.15005 5
------------------------------------------------------------------------
reservation.txt
-------------------------------------
CUST M 1986/01/01 petem@xyz.net Pete Moss
RESERVE H100.15005 2 110.00   
RESERVE H222.15005 2 170.00
RESERVE H200.15010 2 75.00
RESERVE H333.15010 2 200.00     
ENDCUST
CUST M 1957/02/02 pcorn@abc.net Pop Corn
RESERVE H222.15005 10 170.00
RESERVE H333.15010 5 200.00
ENDCUST
CUST F 1958/03/03 pennyl@xyz.net Penny Loafer
RESERVE H444.15001 1 125.00
RESERVE H333.15010 1 200.00
ENDCUST
CUST M 1979/04/04 hgilmore@xyz.net Adam Sandler
RESERVE H100.15005 3 110.00
RESERVE H222.15005 3 170.00
RESERVE H333.15010 3 200.00
ENDCUST
CUST F 1989/05/05 butter@xyz.net Melba Toast
RESERVE H333.15010 2 200.00
RESERVE H444.15015 2 125.00
ENDCUST
-----------------------------------------------------------------------
unsortedNames.txt
----------------------------------------------
N D Street
Faye King
Lenny R Graph
I M Board
Mark R Board
Sal Ed Barr
Grey Wire
Holly Graph
Perry Scope
Bob L Head
Joe King
Holly Wood
Ava Kashun
Jean E Us
Mac A Ronee
E Racer
Hammond Ecks
B B Gunn
Cooke King
Tuck King
N D Carr
Otto Graph
Dee Boat
Anita Goodgrade