awk's most basic function is to browse and extract information based on specified rules in files or strings. It is used to divide a line into several "fields" for processing. Suitable for processing small data.
Example:
Cat /etc/passwd | awk '{FS=":"}$3 < 10 {print $1 "t" $3}' Explanation: The file /etc/passwd is separated by ":". View data less than 10 in the third column, and only display the account number and the third column;
execution method, such as:
awk [-Field-separator] 'commands' input-file(s)
here commands Is the real awk command, [-F domain separator] is optional, awk uses spaces to separate by default, so if you want to browse text with spaces between fields, you do not need to specify this option, but if you browse a file such as passwd, this file will If the domain uses a colon as a separator, you must use the -F option: awk -F : 'commands' input-file
When awk is executed, its browsing marks are $1, $2...$n, this method is called Label the domain. Use $1 and $3 to refer to the 1st and 3rd fields. Note that commas are used to separate the fields, and $0 means to use all fields. For example:
awk '{print $0}' temp.txt > sav.txt
means to print all fields and redirect the results to sav.txt
awk '{print $0}' temp.txt | tee sav.txt
Similar to the above example, the difference is that it will be displayed on the screen
awk '{print $1,$4}' temp.txt
Only print out the first 1 and 4th domain
awk 'BEGIN {print "NAME GRADEn----"} {print $1"t"$4}' temp.txt
means to enter the header, that is, input Add "NAME GRADEn-------------" before the first line of the content, and separate the content with tabs
awk 'BEGIN {print "being"} {print $1} END {print "end"}' temp
Print the information header and information tail at the same time
Conditional operator:
<, <=,==,!= , >=, ~ matches regular expressions, !~ does not match regular expressions
Matching: awk '{if ($4~/ASIMA/) print $0}' temp means that if the fourth field contains ASIMA, then Print the entire
Exact match: awk '$3=="48" {print $0}' temp Only print the record where the 3rd field is equal to "48"
Mismatch: awk '$0 !~ /ASIMA/ ' temp Print the entire record excluding ASIMA
Not equal to: awk '$1 != "asima"' temp
Less than: awk '{if ($1<$2) print $1 "is smaller"} ' temp
Set case: awk '/[Gg]reen/' temp Print the entire record containing Green, or green
Any character: awk '$1 ~/^...a/' temp Print the record in which the fourth character in the first field is a. The symbol '^' represents the beginning of the line, and '.' represents any character
or relational matching: awk '$0~/(abc)|(efg)/ ' temp When using |, the statement needs to be enclosed
AND and relationship: awk '{if ( $1=="a" && $2=="b" ) print $0}' temp
OR or relationship: awk '{if ($1=="a" || $1=="b") print $0}' temp
awk built-in variables:
Example: awk 'END {print NR}' temp in Finally print the number of read records
awk '{print NF, NR, $0} END {print FILENAME}' temp
awk '{if (NR>0 && $4~/Brown/) print $0} ' temp There is at least one record and contains Brown
Another usage of NF: echo $PWD | awk -F/ '{print $NF}' Display the current directory name
Run mode: awk 'Conditional type 1{action 1} condition type 2{action 2} ...' filename
# last | awk '{print $1 "t" $3}' <== View the login data, only display the login name and IP address, and separate awk's built-in variables with [tab]. The meaning of the variable name
NF The total number of fields in each row ($0)
NR What row of data is currently processed by awk
FS specifies the separator, the default is a space bar