Proficient in awk series (18): if, while, switch, for statements of awk process control

Posted by jacksonmj on Sat, 29 Feb 2020 05:46:39 +0100

Go back to:

Process control statement

Note: statement blocks in awk have no scope and are global variables.

if (condition) statement [ else statement ]
expr1?expr2:expr3
while (condition) statement
do statement while (condition)
for (expr1; expr2; expr3) statement
for (var in array) statement
break
continue
next
nextfile
exit [ expression ]
{ statements }
switch (expression) {
    case value|regex : statement
    ...
    [ default: statement ]
}

Code block

{statement}

if...else

# Separate if
if(cond){
    statements
}

# if...else
if(cond1){
    statements1
} else {
    statements2
}

# if...else if...else
if(cond1){
    statements1
} else if(cond2){
    statements2
} else if(cond3){
    statements3
} else{
    statements4
}

Funny question: the wife told the programmer husband to buy a jin of baozi. If you see a watermelon seller, you can buy two. As a result, I bought two steamed buns.

#Semantics of natural language
 Buy a jin of baozi
 If (with watermelon){
    Buy two watermelons
}

#Semantics understood by programmers
 If (no watermelon){
    Buy a jin of baozi
}else{
    Buy two buns
}
awk '
  BEGIN{
    mark = 999
    if (mark >=0 && mark < 60) {
      print "Study slacker"
    } else if (mark >= 60 && mark < 90) {
      print "Not bad"
    } else if (mark >= 90 && mark <= 100) {
      print "Straight A student"
    } else {
      print "Wrong score"
    }
  }
'

Ternary operator?:

expr1 ? expr2 : expr3

if(expr1){
    expr2
} else {
    expr3
}
awk 'BEGIN{a=50;b=(a>60) ? "pass" : "Fail,";print(b)}'
awk 'BEGIN{a=50; a>60 ? b="pass" : b="Fail,";print(b)}' 

switch...case

switch (expression) {
    case value1|regex1 : statements1
    case value2|regex2 : statements2
    case value3|regex3 : statements3
    ...
    [ default: statement ]
}

The switch branch statement in awk has weak function and can only be used for equivalence comparison or regular matching.

break is required at the end of each branch.

{
    switch($1){
        case 1:
            print("Monday")
            break
        case 2:
            print("Tuesday")
            break
        case 3:
            print("Wednesday")
            break
        case 4:
            print("Thursday")
            break
        case 5:
            print("Friday")
            break
        case 6:
            print("Saturday")
            break
        case 7:
            print("Sunday")
            break
        default:
            print("What day?")
            break
    }
}

Branch penetration:

{
    switch($1){
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            print("Weekday")
            break
        case 6:
        case 7:
            print("Weekend")
            break
        default:
            print("What day?")
            break
    }
}

while and do...while

while(condition){
    statements
}

do {
    statements
} while(condition)

while determines the conditions before deciding whether to execute the statements, do...while determines whether to execute the statements next time.

awk 'BEGIN{i=0;while(i<5){print i;i++}}'
awk 'BEGIN{i=0;do {print i;i++} while(i<5)}'

Most of the time, while and do...while are equivalent, but if the first conditional judgment fails, then do...while and while are different.

awk 'BEGIN{i=0;while(i == 2){print i;i++}}'
awk 'BEGIN{i=0;do {print i;i++} while(i ==2 )}'

So, while may not execute once, do...while will execute at least once.

Generally speaking, when, do...while is used less frequently than while.

for cycle

for (expr1; expr2; expr3) {
    statement
}

for (idx in array) {
    statement
}

Topics: Linux shell less