Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Questions

1.  Write an awk script that prints each of the fields in a record in reverse order. The output for the file fruit_prices.txt should look like the following:
Quantity Price/lbs Fruit
100 $0.89 Banana
65 $0.79 Peach
22 $1.50 Kiwi
35 $1.29 Pineapple
78 $0.99 Apple

(HINT: Use the for statement and NF)
2.  Write an awk script that balances a checking account. Your program needs to print the balance in the account every time the user makes a transaction.
The transactions are stored in a file. Each line or record in the file has the following format:
command:date:comment:amount

Here date is the date on which the transaction was made, comment is a string (including embedded spaces) describing the transaction, and amount is the amount of the transaction. The command determines what should be done to the balance with amount. The valid commands are
  B, indicates balance. When this command is encountered, the balance in the account should be set to the transaction amount.
  D, indicates a deposit. When this command is encountered, the transaction amount should be added to the balance.
  C, indicates a check. When this command is encountered, the transaction amount should be subtracted from the balance.
  W, indicates a withdrawal. When this command is encountered, the transaction amount should be subtracted from the balance.

The main difference between the C (check) and the W (withdrawal) commands is that the C (check) command adds an extra field to its records:
command:date:comment:check number:amount

In addition, the B (balance) command uses only two fields:
B:amount

Here amount is the balance amount in the account.
For the purposes of this problem, you need to be concerned with the first field, which contains the command; the second field, which contains the transaction date; and the last field, which contains the transaction amount.
The sample input file looks like the following:
$ cat account.txt
account.txt
B:0
D:10/24/97:inital deposit:1000
C:10/25/97:credit card:101:100
W:10/30/97:gas:21.43
W:10/30/97:lunch:11.34
C:11/02/97:toner:41.45
C:11/04/97:car payment:347.23
D:11/06/97:dividend:687.34
W:11/10/97:emergency cash:200

Your output should look like the following:
10/24/97    1000.00
10/25/97     900.00
10/30/97     878.57
10/30/97     867.23
11/02/97     825.78
11/04/97     478.55
11/06/97    1165.89
11/10/97     965.89
3.  Modify the program you wrote for question 2 to print the ending (total) balance after all input records have been considered. Your output should now look like the following:
10/24/97    1000.00
10/25/97     900.00
10/30/97     878.57
10/30/97     867.23
11/02/97     825.78
11/04/97     478.55
11/06/97    1165.89
11/10/97     965.89
-
Total        965.89

(HINT: Use the END pattern)
4.  Modify the program you wrote in question 3 to support a new command:
  M, indicates the minimum balance. When the balance drops below this minimum balance, a warning should be printed at the end of the output line.

The M (minimum balance) command uses only two fields:
M:amount

Here amount is the balance amount in the account.
The input file changes as follows:
$ cat account.txt h
B:0
M:500
D:10/24/97:inital deposit:1000
C:10/25/97:credit card:101:100
W:10/30/97:gas:21.43
W:10/30/97:lunch:11.34
C:11/02/97:toner:41.45
C:11/04/97:car payment:347.23
D:11/06/97:dividend:687.34
W:11/10/97:emergency cash:200

Your output should be similar to the following:
10/24/97    1000.00
10/25/97     900.00
10/30/97     878.57
10/30/97     867.23
11/02/97     825.78
11/04/97     478.55 * Below Min. Balance
11/06/97    1165.89
11/10/97     965.89
-
Total        965.89

Terms

Field
A set of characters that are separated by one or more field separator characters. The default field separator characters are tab and space.
Numeric expressions
Commands used to add, subtract, multiply, and divide two numbers. Numeric expressions are constructed using the numeric operators + (add), - (subtract), * (multiply), and / (divide).
Field separator
The field separator controls the manner in which an input line is broken into fields. In the shell, the field separator is stored in the variable IFS. In awk, the field separator is stored in the awk variable FS. Both the shell and awk use the default value of space and tab for the field separator.


Previous Table of Contents Next