Shell Awk Variable Assignment

AWK variables: This is our ongoing tutorials on AWK scripting. As we mention earlier AWK is a full pledged language with all statements, arrays, control structures, functions etc. Today we will see how to define a variable in AWK and use it when it’s required. We already covered following AWK concepts

AWK scripting: What is an AWK and how to use it?
AWK scripting: 14 AWK print statment examples
AWK scripting: 8 AWK printf statements examples
AWK scripting: 10 BEGIN and END block examples

What is a Variable?

A variable is defined as storage location to store some value in it so that we can use this in a program. Variables will protect us from varying values in the storage location. This will help us to avoid hardcode a value in to program where ever it’s used. We can define a variable at the start of program and use the variable across the program, if we want to change the value of it, we can change it where we define it and this value will be updated where ever we use that variable.

Defining a variable in AWK

We can define variable in AWK where ever we require. Variables can be used for
Initializations for values
Arithmetic operations
And many more.

For this concept we will use below db.txt file.

cat db.txt
 
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
Edwin 253734 87 97 95
Dayan 24155 30 47

 

Lets learn AWK variables with examples

Example1:  Add all values in column 3 and display for each addition from our db.txt file. Frist value should be first value in column3, second in the list should be first value + second value and so on.
awk ‘BEGIN{a=0}{a=a+$3;print a}’ db.txt
Output:
78
134
172
259
289

In the above example we defined a variable a and initialized to zero in the BEGIN block and did arithmetic operation in main block.
Example2: I don’t want above output, I just want to print a final value once the sum of all value in 3rd column. We can use END block for doing a final print instead of printing per iteration.
awk ‘BEGIN{a=0}{a=a+$3}END{print a}’ db.txt
Output:
289
Example3: Print all the lines which contain numbers greater than VAR1 in it’s column 4

awk ‘BEGIN{VAR1=57}(VAR1<$4){print $0}’ db.txt
 
Output:

Jones 2143 78 84 77
Gondrol 2321 56 58 45
Edwin 253734 87 97 95

These are simple examples to start with Varaiables. In our next post we will see how get inputs from user.

This 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
Jones2143
Gondrol2321
RinRao2122
Edwin2537
Dayan2415

Recommended Reading

Sed and Awk 101 Hacks, by Ramesh Natarajan. I spend several hours a day on UNIX / Linux environment dealing with text files (data, config, and log files). I use Sed and Awk for all my my text manipulation work. Based on my Sed and Awk experience, I’ve written Sed and Awk 101 Hacks eBook that contains 101 practical examples on various advanced features of Sed and Awk that will enhance your UNIX / Linux life. Even if you’ve been using Sed and Awk for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Sed and Awk utilities.

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



Tagged as:Awk Tutorial Examples, Linux Awk Examples, Linux Awk Tutorial, Unix Awk Examples, Unix Awk Tutorial, Variables in Awk

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *