Awk syntax usage
1. Print and Printf statements
Test data emp.dat
A125 Jenny 100 210
A341 Dan 110 215
P158 Max 130 209
P148 John 125 220
A123 Linda 95 210
1.print statement
BEGIN{
print "start"
print "----------------------------"
}
{
print $0
}
END{
print "----------------------------"
print "end"
}
result:
[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat
start
----------------------------
A125 Jenny 100 210
A341 Dan 110 215
P158 Max 130 209
P148 John 125 220
A123 Linda 95 210
----------------------------
end
2.printf statement
formatted output
BEGIN{
printf("start\n")
printf("----------------------------\n")
}
{
# printf("%s\n",$4)
# printf("%5s\n",$4)
# printf("%d\n",$4)
# printf("%5d\n",$4)
# printf("%f\n",$4)
printf("%.4f\n",$4)
}
END{
printf("----------------------------\n")
printf("end\n")
}
The result is:
[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat
start
----------------------------
210.0000
215.0000
209.0000
220.0000
210.0000
----------------------------
end
2. If statement
- if statement
a. Requirements: whether the results are qualified
BEGIN{
score=79
if(score>=60){
print "yes"
}
if(score<60){
print "no"
}
}
- if...else statement
a. Requirement: whether the result is qualified
BEGIN{
score=59
if(score>=60){
print "yes"
}else{
print "no"
}
}
b. Demand: whether the gender is male or female
BEGIN{
sex="girl"
if(sex=="boy"){
print "男"
}else{
print "女"
}
}
- if...else if...else statement
a. Requirement: Which grade is the grade of A, B, C, or D, grade D below 60, grade C 60-79, grade B 80-89, grade A above 90
BEGIN{
score=92
if(score>=90){
print "A"
}else if(score>=80 && score<90){
print "B"
}else if(score>=60 && score<80){
print "C"
}else{
print "D"
}
}
#Optimized code
BEGIN{
score=92
if(score>=90){
print "A"
}else if(score>=80){
print "B"
}else if(score>=60){
print "C"
}else{
print "D"
}
}
Three. While and Do While statement
- while statement
Calculate the cumulative sum of 1-100 numbers
a. Ordinary
BEGIN{
num=100
tot=0
while(num>0){
tot+=num
num--
}
print tot
}
b. Optimization
BEGIN{
num=100
while(num>0){
tot+=num--
}
print tot
}
- do while statement
Press y or n in the system to exit, otherwise it will always execute "Enter y or n"
a. Normal
BEGIN{
print "Enter y or n"
getline data
while( data !~ /^[yn]$/){
print "Enter y or n"
getline data
}
}
b. Optimization
BEGIN{
do{
print "Enter y or n"
getline data
} while( data !~ /^[yn]$/)
}
Four. For statement
- for statement
#Requirement: Calculate the cumulative sum of 1-100 numbers
BEGIN{
for(i=1;i<=100;i++){
tot+=i
}
print tot
}
- for in statement
#Requirement: traverse the array
BEGIN{
arr[0]="linux"
arr[1]="php"
arr[2]="javascript"
arr[3]="java"
arr[4]="python"
for(i in arr){
print i,arr[i]
}
}
Five. Continue and Break statements
- continue statement
#Requirement: skip even 2 and 4
BEGIN{
#1,2,3,4,5
for(i=1;i<=5;i++){
if(i%2==0){
continue
}
tot+=i
}
print tot
}
- break statement
#requires to stop the for loop when i is equal to 3
BEGIN{
#1,2,3,4,5
for(i=0;i<=5;i++){
if(i==3){
break
}
tot+=i
}
print tot
}
6. Delete statement
#Delete array (array) data method
BEGIN{
arr[0]="linux"
arr[1]="php"
arr[2]="java"
# delete arr[0]
delete arr
print arr[2]
}
Seven. exit statement
#Can stop awk script execution
#First paragraph
BEGIN{
print 1
print 2
exit
print 3
print 4
print 5
}
#Second paragraph
BEGIN{
for(i=0;i<5;i++){
if(i==1){
exit
}
print i
}
print 2
print 3
}