Awk Tutorial: Understand Awk Variables with 3 Practical Examples

by Sasikala on January 20, 2010

Linux Awk Tutorial - User-defined and Built-in Awk VariablesThis article is part of the on-going Awk Tutorial and Examples series. Like any other programming languages, Awk also has user defined variables and built-in variables.

In this article let us review how to define and use awk variables.

  • Awk variables should begin with the letter, followed by it can consist of alpha numeric characters or underscore.
  • Keywords cannot be used as a awk variable
  • Awk does not support variable declaration like other programming languages
  • Its always better to initialize awk variables in BEGIN section, which will be executed only once in the beginning.
  • There are no datatypes in Awk. Whether a awk variable is to be treated as a number or as a string depends on the context it is used in.

Now let us review few simple examples to learn how to use user-defined awk variables.

Awk Example 1: Billing for Books

In this example, the input file bookdetails.txt contains records with fields — item number, Book name, Quantity and Rate per book.

$ cat bookdetails.txt
1 Linux-programming 2 450
2 Advanced-Linux 3 300
3 Computer-Networks 4 400
4 OOAD&UML 3 450
5 Java2 5 200

Now the following Awk script, reads and processes the above bookdetails.txt file, and generates report that displays — rate of each book sold, and total amount for all the books sold.

So far we have seen Awk reads the commands from the command line, but Awk can also read the commands from the file using -f option.

Syntax:

$ awk -f script-filename inputfilename

Now our Awk script for billing calculation for books is given below.

$ cat book-calculation.awk
BEGIN {
	total=0;
}
{
	itemno=$1;
	book=$2;
	bookamount=$3*$4;
	total=total+bookamount;
	print itemno," ", book,"\t","$"bookamount;
}
END {
	print "Total Amount = $"total;
}

In the above script,

  • Awk BEGIN section initializes the variable total. itemno, total, book, bookamount are userdefined awk variables.
  • In the Awk Action section, Quantity*bookprice will be stored in a variable called bookamount. Each bookamount will be added with the total.
  • Finally in the Awk END section, total variable will have total amount.

Now execute the book-calculation.awk script to generate the report that displays each book rate and total amount as shown below.

$ awk -f book-calculation.awk bookdetails.txt
1   Linux-programming 	 $900
2   Advanced-Linux 	 $900
3   Computer-Networks 	 $1600
4   OOAD&UML 	 $1350
5   Java2 	 $1000
Total Amount = $5750

Awk Example 2. Student Mark Calculation

In this example, create an input file “student-marks.txt” with the following content — Student name, Roll Number, Test1 score, Test2 score and Test3 score.

$ cat student-marks.txt
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122 38 37 65
Edwin 2537 78 67 45
Dayan 2415 30 47 20

Now the following Awk script will calculate and generate the report to show the Average marks of each student, average of Test1, Test2 and Test3 scores.

$cat student.awk

BEGIN {
	test1=0;
	test2=0;
	test3=0;
	print "Name\tRollNo\t Average Score";

}
{
	total=$3+$4+$5;
	test1=test1+$3;
	test2=test2+$4;
	test3=test3+$5;
	print $1"\t"$2"\t",total/3;
}
END{
	print "Average of Test1="test1/NR;
	print "Average of Test2="test2/NR;
	print "Average of Test3="test3/NR;

}

In the above Awk script,

  • In the Awk BEGIN section all the awk variables are initialized to zero. test1, test2, test3 and total are user-defined awk variables.
  • In the Awk ACTION section, $3, $4, $5 are Test1, Test2 and Test3 scores respectively. total variable is the addition of 3 test scores for each student. The awk variable test1, test2 and test3 has the total scores of each corresponding test.
  • So in the Awk END section, dividing each test total by total number of records (i.e student) will give you the average score.  NR is an Awk built-in variable which gives total number of records in input.

Awk Example 3. HTML Report for Student Details

In the above two example, we have seen awk variable which has numbers as its values. This example shows awk script to generate the html report for the students name and their roll number.

$ cat string.awk
BEGIN{
title="AWK";
print "<html>\n<title>"title"</title><body bgcolor=\"#ffffff\">\n<table border=1><th  colspan=2 align=centre>Student Details</th>";

}
{
name=$1;
rollno=$2;
print "<tr><td>"name"</td><td>"rollno"</td></tr>";

}
END {
    print "</table></body>\n</html>";
}

Use the same student-marks.txt input file that we created in the above example.

$ awk -f string.awk student-marks.txt
<html>
<title>AWK</title><body bgcolor="#ffffff">
<table border=1><th  colspan=2 align=centre>Student Details</th>
<tr><td>Jones</td><td>2143</td></tr>
<tr><td>Gondrol</td><td>2321</td></tr>
<tr><td>RinRao</td><td>2122</td></tr>
<tr><td>Edwin</td><td>2537</td></tr>
<tr><td>Dayan</td><td>2415</td></tr>
</table></body>
</html>

We can store the above output, which gives the following html table. In the above script, variable called name and rollno are string variable, because it is used in string context.

Student Details
Jones 2143
Gondrol 2321
RinRao 2122
Edwin 2537
Dayan 2415
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
  2. 4 Awk If Statement Examples ( if, if else, if else if, :? )
  3. AWK Arrays Explained with 5 Practical Examples
  4. Awk Introduction Tutorial – 7 Awk Print Examples
  5. Caught In the Loop? Awk While, Do While, For Loop, Break, Continue, Exit Examples
  

Vim 101 Hacks Book

{ 2 trackbacks }

Awk Introduction Tutorial – 7 Awk Print Examples
March 8, 2010 at 11:47 pm
AWK Arrays Explained with 5 Practical Examples
March 10, 2010 at 12:04 am

{ 8 comments… read them below or add one }

1 Rishabh January 20, 2010 at 4:31 am

hi,
Can the AWK command work on data that is comma separated or for that matter any other special character(say pipe)?

2 Chris F.A. Johnson January 20, 2010 at 1:29 pm

Anything can be used as the separator by assigning that value to the FS variable, either in the BEGIN block or on the command line:

awk -F\| ‘….’ ## assigment on the command line

awk ‘BEGIN {FS = “|”} …’ ## assigment in the BEGINblock

3 Rishabh January 20, 2010 at 10:01 pm

Thanks Chris.

4 S.RAGHU January 21, 2010 at 11:37 pm

You can even work with data files with no delimiters by using substring function.
Eg.
name=substring($0,1,18);
id=substring($0,19,5);
,etc.
where $0 refers to the whole line.

5 S.RAGHU January 21, 2010 at 11:38 pm

$0 refers to the whole line of input data.

6 Chris F.A. Johnson January 22, 2010 at 2:15 am

There is no substring function; the function is substr().

7 S.RAGHU January 22, 2010 at 3:15 am

Thanks Chris. Sorry for the slip up.

8 boo January 25, 2010 at 2:17 pm

awwww(k), people should really be using perl(*)

(*) Or other appropriate, but modern, interpreter. python, whatever pleases you.

Leave a Comment

Previous post:

Next post: