Linux shell awk process control statement (if, for, while, do) details

  
 

In the while, do-while, and for statements of linux awk, break, continue statements are allowed to control the flow of the process, and statements such as exit are allowed to exit. Break breaks the currently executing loop and jumps outside the loop to execute the next statement. If is the process selection usage. In awk, flow control statements, grammatical structures, and c language types. The following is the usage of each statement.


1. Conditional statement (if)

if(expression) #if ( Variable in Array ) statement 1else statement 2

Format "Statement 1" can be multiple statements. If you are comfortable reading Unix awk for your convenience, you'd better enclose multiple statements with {}. The Unix awk branching structure allows nesting in the format:

if(expression)

{statement 1}

else if(expression){statement 2} Else{statement 3}

[chengmo@localhost nginx]# awk 'BEGIN{ test=100;if(test>90){print "very good";}else if(test>60){print "good";}else{print "no pass";}}'

very good


Each command statement can be followed by “;” End of the number.


Two. Loop statement (while, for, do)

1.while statement

Format:

while (Expression ())

{statement}

example:

[chengmo@localhost nginx]# awk 'BEGIN{ test=100;total=0;while(i<=test ) {total+=i;i++;}print total;}'5050

2.for loops

for loops come in two formats:

Format 1:

for(variable in array)

{statement}

example:

[chengmo@localhost nginx]# awk 'BEGIN{ for(k in ENVIRON){ Print k"="ENVIRON[k];}}'

AWKPATH=.:/usr/share/awkOLDPWD=/home/web97SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpassSELINUX_LEVEL_REQUESTED=SELINUX_ROLE_REQUESTED =LANG=zh_CN.GB2312

. . . . . .

Note: ENVIRON is an awk constant and is a sub-typical array.

Format 2:

for(variable; condition; expression)

{statement}

example:

[chengmo@ Localhost nginx]# awk 'BEGIN{ total=0;for(i=0;i<=100;i++){total+=i;}print total;}'

5050

3 .do loop

Format:

do

{statement}while(condition)

example:

[chengmo@localhost nginx ]# awk 'BEGIN{ total=0;i=0;do{total+=i;i++;}while(i<=100)print total;}'5050


The above is The awk flow control statement, as you can see from the grammar, is the same as the c language. With these statements, many shell programs can be handed over to awk, and the performance is very fast.


break When a break statement is used in a while or a for statement, it causes the program loop to exit. Continue Causes the program loop to move to the next iteration when the continue statement is used for a while or a for statement. Next can cause the next input line to be read and returned to the top of the script. This avoids performing other operations on the current input line. The exit statement causes the main input to loop out and transfer control to END if END is present. If the END rule is not defined, or the exit statement is applied in END, the execution of the script is terminated.

Three, performance comparison

[chengmo@localhost nginx]# time (awk 'BEGIN{ total=0;for(i=0;i<=10000;i++){total+=i ;}print total;}')50005000

real 0m0.003suser 0m0.003ssys 0m0.000s[chengmo@localhost nginx]# time(total=0;for i in $(seq 10000);do total= $(($total+i));done;echo $total;)50005000

real 0m0.141suser 0m0.125ssys 0m0.008s


To achieve the same function, You can see that the performance of awk is 50 times that of the shell!

Copyright © Windows knowledge All Rights Reserved