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!
Introduction to Soft InterruptsInsert processing that can be deferred from the hard interrupt handl
When dealing with Oracle problems in the actual production environment, sometimes you need a remote
GRUB is the default bootloader for most Linux systems. You can choose to enter different operating s
There are some scenario where system admin wants only few users should be allowed to transfer files
Hard disk bad track detection tutorial under Linux
Introduction to linux environment variables
Shell monitoring linux system file changes
Linux system installation Eclipse method introduction
Let Linux and Windows coexist with the same machine
Let Linux run Windows programs Wine
Clearing the record of operation information under Linux system
How do I find a solution to my problem using the Win10 error report?
How is the invitation code? The system home teaches you to play Cheetah free WiFi
Have to prevent - beware of Win XP's "automatic update"
Win7 graphics hardware acceleration how to open
Did not receive Win10 push how to do Win7/8 did not receive win10 upgrade prompt solution
Asking people is not as good as the scanner does not recognize the USB port
Win10 10074 preview version of the start menu can not open the solution
Windows7 system to modify the interface text size skills (graphic tutorial)
Reinstalling IIS doesn't have to be a simple step to get
Win8.1 Update how to install Simplified Chinese language package