Basic usage of awk:
Awk [parameter] ‘BUGIN{} pattern{} END{}’ file…
Parameters: -f Specifies the file name (the contents of the 'BUGIN{} pattern{} END{}' part can be saved to the file, called with -f) -f specifies the delimiter to be used when processing the line example: awk -f: '{print $1,$3}' /etc/passwd -v var=value Customizing variables FS Input field separator example: awk -v FS=':' '{print $1,$3}' /etc/passwd OFS Example: Awk -v FS=':' -v OFS=',' {print $1,$3}' /etc/passwd 'RS Input record separator ORS Output record separator NF Awk '{print NF}' /etc/fstab NR Example of recording row numbers awk '{print NR}' /etc/fstab /etc/inittab FNR Example of recording row numbers awk '{print NR}' /etc/fstab /etc/inittab FNR Awk '{print FNR}' /etc/fstab /etc/inittab FILENAME Example: Awk '{print FILENAME}' /etc/fstab ARGC number of files Awk 'BEGIN{print ARGC}' /etc/passwd /etc/group ARGV array file names Example: awk 'BEGIN{print ARGV[1]}' /etc/passwd Operator: ==,! =, >, <, > =, < =, and &&, or | |, non! ~ right and left to match, configuration to the operation! Awk -f: '$0 ~ /^root/{print $1,$3}' /etc/passwd? Awk -f: '{$3<500? U=" system user ":U=" common user "; Awk -f: '/^root/,/^nobody/' /etc/passwd if(expression){statement; awk -f: '/^root/,/^nobody/' /etc/passwd if(expression){ . } [else} {statements] cases: the awk - F: '{the if ($3 > = 500) {print $1 "is a regular user"} else {print $1 "is the system user"}}'/etc/passwd if (expression) {statement; . }else if(expression){statement; . } else {statement; . } example: awk 'BEGIN{test=100; If (test>90){print"very good"}else if(test>60){print "good"}else{print "no pass"}}' while(expression){statement; . } awk -f: '/^root/{I =1; while(i<=NF){print $i,length($i); I++}}' /etc/passwd do{statement; . }while(expression) Regardless of whether the result of the expression is true or false, the statement for(statement; The expression; Statement){statement; . } example: awk 'BEGIN{sum=0; for(i=1; i<=100; I ++){sum+= I}print sum}' break continue next break awk -f: '/^root/{I =1; while(i<=NF){if(i==4){next}print $i,length($i); I++}}' /etc/passwd array example: Statistical TCP monitor service of state statistics netstat - tan | awk '/ ^ TCP / {state [$NF] + +} END {for (I in the state) {print I, state [I]}}' cases: Awk '{ips[$1]++}END{for(IP in ips){print IP "= "ips[IP]}}' /var/log/httpd/access_log BEGIN{} Operations performed before file processing END{} Operations performed after file processingCopy the code