Loops in AWK

Walden Systems Geeks corner Programming loops in AWK programming tutorial howto Rutherford NJ New Jersey NYC New York North Bergen County
Awk is text processing program that are mainstays of the UNIX/Linux programmer's toolbox.

Loops are used to execute a set of actions in a repeated manner. The loop execution continues as long as the loop condition is true. Awk looping statements are used to perform a set of actions again and again in succession. Awk keeps executing a statement as long as the loop condition is true. Just like the C programming language, awk supports various looping statements. Loops are used to execute a set of actions in a repeated manner. The loop execution continues as long as the loop condition is true. AWK supports for loops, while loops, and do loops.

While loops

The awk while loop checks the condition first; if the condition is true, it executes the actions. After executing all the actions, the condition is checked again, and if it is true, the actions are performed again. This process is repeated until the condition becomes false. Like Python and Perl, if the condition returns false in the first iteration, the actions are never executed. Also, if the condition is always true, the actions will execute in an infinite loop.

While loop that never executes
i = 5
while ( i <= 4 ) 
{
	print $i
	++i 
}


The above code will output nothing.


While loop that loops infinitely
i = 1
b = 2
while ( b <= 4 ) 
{
	print $i
	++i 
}


The above code will output 123456.... and never stop.


While loop
i = 1
while ( i <= 4 ) 
{
	print $i
	++i 
}


The above code will output 1234.



Do loops

The do loop is a modified version of the while loop. In a do loop, the test condition is evaluated at the end of the loop assuring that the code block executes at least once. The do loop is an exit-controlled loop, meaning that the condition is checked at exit. To separate the loop from the rest of the code, we use the BEGIN keyword and enclose the do loop in curly braces. Just like the while loop, if the test condition always evaluates to true, the loop will never exit out.

Do loop that never stops
BEGIN
{
    i = 1
    do
    {
	print $i
	++i 
    }
    while ( i <= 4 )
}


The above code will output 123456.... and never stop.


Do loop where the test fails immediately
BEGIN
{
    i = 5
    do
    {
	print $i
	++i 
    }
    while ( i <= 4 )
}


The above code will output 5.


Do loop that never stops
BEGIN
{
    i = 1
    b = 2
    do
    {
	print $i
	++i 
    }
    while ( b <= 4 )
}


The above code will output 123456...


Do loop
BEGIN
{
    i = 1
    do
    {
	print $i
	++i 
    }
    while ( i <= 4 )
}


The above code will output 1234.


For loops

The for loop offers a more condensed syntax that gets the same result as a while loop. Although it looks more difficult, this syntax is much easier to use and makes sure that we provide all the required elements of a loop. The awk for statement starts by executing initialization, then checks the condition; if the condition is true, it executes the actions, then does the increment or decrement. As long as the condition is true, awk repeatedly executes the action and then the increment/decrement. Similar to the do loop, the for loop, we use the BEGIN keyword and separate the loop by enclosing it in curly braces.

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


The above code outputs 1234.