Beruflich Dokumente
Kultur Dokumente
Awk Programming
1. lets first start with one line programs.
1.1. Display just the names, genders and endsem marks of:
(a) all students (b) the first 3 students only (c) the last student
1.2 Store the male students in males.txt and the females in females.txt.
1.3. Compute the total mark for each student and display it along with their names.
1.4. Who is the topper? Which students scored above the class average?
2. Little XYZ has joined our MNNIT and he is very new to the grading system present here. He has
finished his first semester and has got some grades. But he doesn’t know how to compute the
GPA. Below is a list of subjects which he has taken, grade he obtained and the subject credits.
Can you help him to compute his GPA using awk script?
(Hint : calculate the GPA based off of the equation sum of grade * credit hours / sum of credit hours)
3. Given the following shell function
lspids()
{
/bin/ps -ef | grep “$1”| grep -v grep ;
}
OR
lspids()
{
/bin/ps -auwx 2> /dev/null | grep “$1”| grep –v
grep ;
}
make the necessary changes so that when the function is executed as follows
$ lspid -h ssh
the output looks like this:
$ lspid ssh
Here you are using ssh as the word specified to grep, but your function should be able to use any
word as an argument. You have to use awk to print PID and location of process.
4. Consider the price list for a small fruit market. The list is stored in the file
fruit_prices.txt:
Pineapple 1.29 84
Apple 0.99 21
Mango 2.20 52
a) 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:
65 $0.79 Peach
22 $1.50 Kiwi
35 $1.29 Pineapple
78 $0.99 Apple
b) Write a shell script which prints the total quantity and sum of purchase.
5. Write an awk script that checking balance 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
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
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
6. Modify the program you wrote for question5 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