Generating Columnar Reports
This section describes a small-scale business application that produces reports with dollar amounts. While this application doesn’t introduce any new material, it does emphasize the data processing and reporting capabilities of awk. (Surprisingly, some people do use awk to write small business applications.)
It is presumed that a script exists for data entry. The data-entry script has two jobs: the first is to enter the customer’s name and mailing address for later use in building a mailing list; the second is to record the customer’s order of any of seven items, the number of items ordered, and the price per item. The data collected for the mailing list and the customer order were written to separate files.
Here are two sample customer records from the customer order file:
Charlotte Webb P.O N61331 97 Y 045 Date: 03/14/97 #1 3 7.50 #2 3 7.50 #3 1 7.50 #4 1 7.50 #7 1 7.50 Martin S. Rossi P.O NONE Date: 03/14/97 #1 2 7.50 #2 5 6.75
Each order covers multiple lines, and a blank line separates one order from another. The first two lines supply the customer’s name, purchase order number and the date of the order. Each subsequent line identifies an item by number, the number ordered, and the price of the item.
Let’s write a simple program that multiplies the number of items by the price. The script can ignore the first two lines of each record. We only want to read the lines where an item is specified, as in the following example.
awk '/^#/ { amount = $2 * $3 ...