Main index

Introducing UNIX and Linux


Awk

Overview
What is 'awk'?
Invoking 'awk'
Naming the fields
Formatted output
      Operators used by Awk
Patterns
Variables
      Accessing Values
      Special variables
Arguments to 'awk' scripts
Arrays
Field and record separators
Functions
      List of Awk functions
Summary
Exercises

Exercises

  1. A railway company operates trains that travel between a number of cities. The company offers three types of service: local, fast and express. Its fares are based on 10p per km travelled per passenger for local trains, 12p for fast trains, and 15p for express. The company keeps a log of all journeys made. For each year this data is kept in a file (trainlog, say), which contains a number of fields. These are, in order, the departure city, the destination city, the distance travelled (in kilometres), the number of passengers carried, and the service (local, fast or express). The final two fields represent the day and month the journey took place. A typical part of the log file might look like:

        ...
    Edinburgh Glasgow 71 23 local 14 5
    Aberdeen London 805 675 express 14 5
    Manchester Birmingham 128 534 fast 15 5
    Exeter Exmouth 8 112 local 15 5
        ...
    

    The costs to the company of running trains are a fixed cost of 100 pounds per journey made plus 5 pounds per km travelled. Write Awk scripts to take input from trainlog and display the following information:

    1. The number of trains run.
    2. The number of trains run in May.
    3. The number of fast trains run in May.
    4. The total number of passengers carried in the year.
    5. The total fares collected in the year.
    6. The percent of revenue which was generated by local trains.
    7. For each train, the profit or loss made on the journey; the output should be a sequence of lines formatted so:
         ...
      14/5 Edinburgh-Glasgow: loss 291.70
      14/5 Aberdeen-London: profit 77381.25
      15/5 Manchester-Birmingham: profit 7462.24
      15/5 Exeter-Exmouth: loss 50.40
         ...
      

    View solution

  2. The Anytown and Blankshire Historical Society has decided to computerise its membership records. There are 3 classes of membership:

    • Annual, renewable on the anniversary of joining and subject to a fee of 10.00 pounds each year.
    • Life, subject to a single payment of 250.00 pounds.
    • Honorary, which gives the same rights and privileges as Life membership, but is awarded by the Committee and no fee is payable.

    Its membership secretary proposes to store its membership records in a file containing single-line records. Each record contains a number of colon-separated fields, the number of which depend on the class of membership. For annual members, the fields have the following meaning:

    1. Surname
    2. First name(s) or initials
    3. Class of membership, the string 'annual'
    4. Address
    5. Home phone number
    6. Date of first joining (dd/mm/yy)
    7. Date renewal due

    For life and honorary members, field 3 is 'life' and 'honorary' respectively, and there are only six fields. For example,

    Bloggs:Fred:annual:1 High Street:1234:03/12/90:03/12/97
    Smith:John J.:annual:2 High Street::13/01/97:13/01/98
    Doe:Jane:life:3 High Street:123 4567:22/02/93
    Jones:Cllr. A.:honorary:New House:123 2345:22/02/93
    

    Write shell scripts to read a membership file from standard input and produce the following information:

    1. A list of members' names, sorted by category of membership, then alphabetically by surname.
    2. A list of annual members whose membership has expired and is due for renewal.
    3. The total number of annual members due to renew in the current year.
    4. The total dues paid already by each member during their membership of the society.
    5. A list of honorary members who will have been of 10 years' standing in the current year.
    6. Combine (1--5) to produce a single shell script to display a comprehensive report. Take care to include messages in the output so that the report is easy to read.
  3. For each of these tasks, ask how else you might solve the problem under UNIX, for instance with Grep or Sed. Is Awk the most appropriate tool?


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck