Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev
Chapter 7: awk Revisited
Next

User Defined variables in awk

You can also define your own variable in awk program, as follows:

$ cat > math1
{
no1 = $1
no2 = $2
ans = $1 + $2
print no1 " + " no2 " = " ans
}

Run the program as follows
$ awk -f math1
1 5
1 + 5 = 6

In the above program, no1, no2, ans all are user defined variables. Value of first and second field are assigned to no1, no2 variable respectively and the addition to ans variable. Value of variable can be print using print statement as, print no1 " + " no2 " = " ans. Note that print statement prints whatever enclosed in double quotes (" text ") as it is. If string is not enclosed in double quotes its treated as variable. Also above two program takes input from stdin (Keyboard) instead of file.

Now try the following awk program and note down its output.

$ cat > bill
{
total = $3 * $4
recno = $1
item = $2
print recno item " Rs." total
}

Run it as
$ awk -f bill inven
1.Pen Rs.100
2.Pencil Rs.20
3.Rubber Rs.10.5
4.Cock Rs.91

Here we are printing the total price of each product (By multiplying third field with fourth field). Following program prints total price of each product as well as the Grand total of all product in the bracket.

$ cat > bill1
{
total = $3 * $4
recno = $1
item = $2
gtotal = gtotal + total
print recno item " Rs." total " [Total Rs." gtotal "] "

}

Run the above awk program as follows:
$ awk -f bill1 inven
1.Pen Rs.100 [Total Rs.100]
2.Pencil Rs.20 [Total Rs.120]
3.Rubber Rs.10.5 [Total Rs.130.5]
4.Cock Rs.91 [Total Rs.221.5]

In this program, gtotal variable holds the grand total. It adds the total of each product as gtotal = gtotal + total. Finally this total is printed with each record in the bracket. But their is one problem with our script, Grand total mostly printed at the end of all record. To solve this problem we have to use special BEGIN and END Patterns of awk. First take the example,

$ cat > bill2
BEGIN {
   print "---------------------------"
   print "Bill for the 4-March-2001. "
   print "By Vivek G Gite. "
   print "---------------------------"
}

{
   total = $3 * $4
   recno = $1
   item = $2
   gtotal += total
   print recno item " Rs." total
}

END {
   print "---------------------------"
   print "Total Rs." gtotal
   print "==========================="
}

Run it as
$awk -f bill2 inven
---------------------------
Bill for the 4-March-2001.
By Vivek G Gite.
---------------------------
1.Pen Rs.100
2.Pencil Rs.20
3.Rubber Rs.10.5
4.Cock Rs.91
---------------------------
Total Rs.221.5
===============

Now the grand total is printed at the end. In above program BEGIN and END patters are used. BEGIN instruct awk, that perform BEGIN actions before the first line (Record) has been read from database file. Use BEGIN pattern to set value of variables, to print heading for report etc. General syntax of BEGIN is as follows
Syntax:
BEGIN {
                action 1
                action 2
                action N
             }

END instruct awk, that perform END actions after reading all lines (RECORD) from the database file. General syntax of END is as follows:

END {
                 action 1
                 action 2
                 action N
          }

In our example, BEGIN is used to print heading and END is used print grand total.


Prev
Home
Next
Doing arithmetic with awk
Up
Use of printf statement