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

Loops in awk

For loop and while loop are used for looping purpose in awk.
Syntax of for loop
Syntax:
for (expr1; condition; expr2)
{
     Statement 1
     Statement 2
     Statement N
}

Statement(s) are executed repeatedly UNTIL the condition is true. BEFORE the first iteration, expr1 is evaluated. This is usually used to initialize variables for the loop. AFTER each iteration of the loop, expr2 is evaluated. This is usually used to increment a loop counter.

Example:

$ cat > while01.awk
BEGIN{
   printf "Press ENTER to continue with for loop example from LSST v1.05r3\n"
}
{
sum = 0
i = 1
for (i=1; i<=10; i++)
{
  sum += i; # sum = sum + i
}
printf "Sum for 1 to 10 numbers = %d \nGoodbuy!\n\n", sum
exit 1
}

Run it as follows:
$ awk -f while01.awk
Press ENTER to continue with for loop example from LSST v1.05r3
Sum for 1 to 10 numbers = 55
Goodbuy

Above for loops prints the sum of all numbers between 1 to 10, it does use very simple for loop to achieve this. It take number from 1 to 10 using i variable and add it to sum variable as sum = previous sum + current number (i.e. i).

Consider the one more example of for loop:

$ cat > for_loop
BEGIN {
   printf "To test for loop\n"
   printf "Press CTRL + C to stop\n"
 }
 {
  for(i=0;i<NF;i++)
  {
       printf "Welcome %s, %d times.\n" ,ENVIRON["USER"], i
  }
}

Run it as (and give input as Welcome to Linux!)
$ awk -f for_loop
To test for loop
Press CTRL + C to Stop
Welcome to Linux!
Welcome vivek, 0 times.
Welcome vivek, 1 times.
Welcome vivek, 2 times.

Program uses for loop as follows:

for(i=0;i<NF;i++)Set the value of i to 0 (Zero); Continue as long as value of i is less than NF (Remember NF is built in variable, which mean Number of Fields in record); increment i by 1 (i++) 
printf "Welcome %s, %d times.\n" ,ENVIRON["USER"], iPrint "Welcome...." message, with user name who is currently log on and value of i. 

Here i++, is equivalent to i = i + 1 statement. ++ is increment operator which increase the value of variable by one and -- is decrement operator which decrease the value of variable by one. Don't try i+++, to increase the value of i by 2 (since +++ is not valid operator), instead try i+= 2.

You can use while loop as follows:
Syntax:
while (condition)
{
      statement1
      statement2
      statementN
      Continue as long as given condition is TRUE
}

While loop will continue as long as given condition is TRUE. To understand the while loop lets write one more awk script:

$ cat > while_loop
{
no = $1
remn = 0
while ( no > 1 )
 {
    remn = no % 10
    no /= 10
    printf "%d" ,remn
 }
 printf "\nNext number please (CTRL+D to stop):";
}

Run it as
$awk -f while_loop
654
456
Next number please(CTRL+D to stop):587
785
Next number please(CTRL+D to stop):

Here user enters the number 654 which is printed in reverse order i.e. 456. Above program can be explained as follows:

no = $1Set the first fields ($1) value to no.
remn = 0Set remn variable to zero
{Start the while loop
while (no > 1)Continue the loop as long as value of no is greater than one
remn = no % 10Find the remainder of no variable, and assign result to remn variable.
no /= 10Divide the no by 10 and store result to no variable.
print "%d", remnPrint the remn (remainder) variables value.
}End of while loop, since condition (no>1) is not true i.e false condition..
printf "\nNext number please (CTRL+D to stop):";    Prompt for next number


Prev
Home
Next
if condition in awk
Up
Real life example in awk