Article Directory
1 Overview
AWK is an excellent text processing tool, one of the most powerful data processing engines available in Linux and Unix environments. This programming and data manipulation language (which takes its name from the initials of its founders Alfred Aihor, Peter Weinberg, and Brian Collingham)'s maximum capabilities depend on what a person knows. possessed knowledge. The new version nawk and gawk generated by the improvement of awk, now gawk is used in daily use under the Linux system by default, and the source of the awk being applied can be viewed with the command (ls -l /bin/awk)
2 Basic Usage
awk '{pattern + action}' <file>
pattern indicates what to look for in the data
action is a series of commands to be executed
Awk divides a line into multiple fields by specifying a delimiter, and uses $1, $2...$n to represent the first field, the second field...the nth field in turn.
An example is the following file. The format we already know is as follows. The files after PSS and RSS that you want to filter, the corresponding fields are 3 and 6. Use the following commands to filter out the contents of the third field and the sixth field.
TOTAL PSS: 102206 TOTAL RSS: 127132 TOTAL SWAP (KB): 0
TOTAL PSS: 102438 TOTAL RSS: 127364 TOTAL SWAP (KB): 0
TOTAL PSS: 102494 TOTAL RSS: 127420 TOTAL SWAP (KB): 0
TOTAL PSS: 102578 TOTAL RSS: 127504 TOTAL SWAP (KB): 0
TOTAL PSS: 102610 TOTAL RSS: 127536 TOTAL SWAP (KB): 0
TOTAL PSS: 102558 TOTAL RSS: 127484 TOTAL SWAP (KB): 0
TOTAL PSS: 102378 TOTAL RSS: 127304 TOTAL SWAP (KB): 0
TOTAL PSS: 102594 TOTAL RSS: 127520 TOTAL SWAP (KB): 0
TOTAL PSS: 102554 TOTAL RSS: 127480 TOTAL SWAP (KB): 0
TOTAL PSS: 102262 TOTAL RSS: 127188 TOTAL SWAP (KB): 0
awk '{print $3, $6}' hal_PSS.txt
2.1 Delimiter
Awk's default separator is space and tab. In the above example, if you want to remove the comma, just add -F
awk -F ':|,' '{print $3 $6}' hal_PSS.txt
Colon (:) and comma (,) are specified here as separators
2.2 Condition judgment
Print out whether the third field is true or not if the number is less than 102262
awk '{print $3<102262}' hal_PSS.txt
The print result is
1
0
0
0
0
0
0
0
0
0
Print out the information of the line whose third field satisfies less than 102262
awk '$3 <102262 {print $0}' hal_PSS.txt
The print result is as follows
TOTAL PSS: 102206 TOTAL RSS: 127132 TOTAL SWAP (KB): 0
2.3 Statistical calculations
maximum value
awk 'BEGIN {max=0} {if($3>max) max=$3} END {print "max PSS:", max}' hal_PSS.txt
minimum value
awk 'BEGIN {min=102262} {if($3<min) min=$3} END {print "min PSS:", min}' hal_PSS.txt
average value
awk 'BEGIN {sum=0} {sum+=$3} END {print "mean steer:", sum/NR}' hal_PSS.txt