An Awk Primer/Control Structures – Wikibooks, open books for an open world

Muggles' Guide to Harry Potter/Characters/Auntie Muriel

Awk helps management buildings just like these utilized in C, together with:

  • if ... else
  • whereas
  • for
  • break
  • proceed

There are additionally management buildings totally different from these utilized in C:

  • for (x in a) – loop in array
  • subsequent – go to the following enter document
  • exit – cease processing the enter information, execute END operations, if specified, and end.

if … else[edit]

The syntax of “if … else” is:

   if ()  [else ]

The “else” clause is non-obligatory. The “situation” might be any expression mentioned within the part on sample matching, together with matches with common expressions. For instance, take into account the next Awk program:

{
   if ($1=="inexperienced") print "GO";
   else if ($1=="yellow") print "SLOW DOWN";
   else if ($1=="crimson") print "STOP";
   else print "SAY WHAT?";
}

(Right here, semicolons are non-obligatory and might be omitted.)

By the best way, for check functions this program might be invoked as:

   echo "crimson" | awk -f pgm.txt

—the place “pgm.txt” is a textual content file containing this system.

The “motion” clauses can include a number of statements, contained by curly brackets (“{}”).

The syntax for “whereas” is:

   whereas () 

The “motion” is carried out as lengthy the “situation” exams true, and the

“situation” is examined earlier than every iteration. The situations are the identical as for the “if … else” assemble. For instance, since by default an Awk variable has a price of 0, the next Awk program may print the numbers from 1 to 20:

  BEGIN {x=0; whereas(x<=20) {++x; print x}}
  • The "for" loop is extra versatile. It has the syntax:
   for (;;) 

For instance, the next "for" loop prints the numbers 10 via 20 in increments of two:

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

That is equal to:

    i=10
    whereas (i<=20) {
        print i
        i+=2
    }

The C programming language has an analogous "for" assemble, with an fascinating characteristic in that a number of actions might be taken in each the initialization and end-of-loop actions, just by separating the actions with a comma. Most implementations of Awk, sadly, don't assist this characteristic.

The "for" loop has an alternate syntax, used when scanning via an array:

   for ( in ) 

Given the instance used earlier:

   my_string = "joe:frank:harry:invoice:bob:sil";    break up(my_string, names, ":");

—then the names might be printed with the next assertion:

   for (idx in names) print idx, names[idx];

This yields:

   2 frank
   three harry
   four invoice
   5 bob
   6 sil
   1 joe

Discover that the names are not printed within the correct order. One of many traits of the sort of "for" loop is that the array isn't scanned in a predictable order.

Unconditional management statements[edit]

Awk defines 4 unconditional management statements: break, proceed, subsequent, and exit.

The statements break and proceed are strictly related to the whereas and for loops:

  • break: Causes a soar out of the loop.
  • proceed: Forces the following iteration of the loop.

The statements subsequent and exit management Awk's enter scanning:

  • subsequent: Causes Awk to right away get one other line of enter and start scanning it from the primary match assertion.
  • exit: Causes Awk to cease studying its enter, execute END operations, if specified, and end.

Leave a Reply

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