The for loop statement

The AWK for statement functionally works the same as the AWK while loop; however, the for statement syntax is much easier to use. Its syntax is as follows:

for ( initialization; conditional-expression; increment / decrement )

action statements

The description of different keywords and statement used in for loop syntax above is as follows:

  • initialization: Sets the initial value for the counter variable
  • conditional-expression: States a condition that is tested at the top of the loop
  • increment/decrement: Increment/decrement the counter each time at the bottom of the loop, just before testing the conditional-expression again

For statement starts by performing initialization action, then it checks the conditional-expression. If the conditional-expression evaluates to true, it executes the action statements specified in body part. Thereafter, it performs increment or decrement operation. Then it checks the conditional-expression, if it is true then AWK again executes the action statement and the increment/decrement.

The loop executes as long as the conditional-expression evaluates to true as illustrated in Figure 7.7:

Figure 7.7: A for loop statement flowchart

Let's understand the working of for loop with some examples. In the following example, we will print the numbers from 1 to 10. First, we initialize the counter i to 1, then, in the conditional-expression, we put the condition if the counter is less than or equal to 10. If the condition is true, the statements given in the body block are executed.

Here, we print the counter value and then increment the counter. When all statements in the body block are executed, the condition is again evaluated, and, if it is true, the body block is again executed:

$ vi for1.awk

BEGIN {
for ( i = 1; i <=10; i++ )
print i
}

$ awk -f for1.awk

The output on execution of the preceding code is as follows:

1
2
3
4
5
6
7
8
9
10

In the following example, we will print four fields of each record, one field per line, from the employee database emp.dat to generate labels for employees. The body of the for loop is a compound statement containing the action statement and other conditional if...else...if statements. First, the value of i is initialized to 1. Then the for statement tests whether i is less than or equal to 4. If this is true, the action statements given inside the for body are executed. Then, as the last action statement, i is incremented using i++ and the loop repeats. The loop terminates when i attains the value of 5:

$ vi for2.awk

{
for ( i =1; i <10 ; i++ )
{
if ( i == 1 )
{ printf "First Name : %s ", $i }
else if ( i == 2 )
{ printf "Last Name : %s ", $i }
else if ( i == 3 )
{ printf "Phone number : %s ", $i }
else if ( i == 4 )
{ printf "Email id : %s ", $i
printf "************************************ "
}
}
}

$ awk -f for2.awk emp.dat

The output on execution of the preceding code is as follows:

First Name : Jack
Last Name : Singh
Phone number : 9857532312
Email id : [email protected]
************************************
First Name : Jane
Last Name : Kaur
Phone number : 9837432312
Email id : [email protected]
************************************
First Name : Eva
Last Name : Chabra
Phone number : 8827232115
Email id : [email protected]
************************************
First Name : Amit
Last Name : Sharma
Phone number : 9911887766
Email id : [email protected]
************************************
First Name : Julie
Last Name : Kapur
Phone number : 8826234556
Email id : [email protected]
************************************
..........
..........

In the next example, we will use the marks.txt sample file to do the total of marks obtained by the student in different subjects. For each line, the program has to add the values of field 2 through to the last field. So, we first initialize i=2 before entering the loop and test using conditional-expression if it has reached the last field in the record (i <= NF). NF represents the total number of fields in each record, as follows:

$ vi for3.awk

{
total=0;
for ( i=2; i <=NF; i++ )
{
total = total + $i;
}
print "Student Name : ",$1, " ", "Total Marks : ", total;
}

$ awk -f for3.awk marks.txt

The output on execution of the preceding code is as follows:

Student Name :  ram        Total Marks :  375
Student Name : amit Total Marks : 323
Student Name : vijay Total Marks : 473
Student Name : satvik Total Marks : 386
Student Name : akshat Total Marks : 353
Student Name : rishi Total Marks : 407
Student Name : tushar Total Marks : 359
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.5.86