A brief introduction, awk is a powerful text analysis tool. Compared with grep search and sed editing, awk is particularly powerful when it analyzes data and generates reports. This is a necessary basic skill for us to play Linux. If To have a more detailed understanding of his life experience, just search it yourself. Regarding the knowledge of tools, the author tries to briefly explain each knowledge point and gives examples.
Simply put, awk reads the file line by line, slices each line with spaces as the default delimiter, and then performs various analysis and processing on the cut parts.
Usage:
awk [options] 'scripts' file1,file2...
awk [options] 'pattern {action}' file1,file2...
options are options supported by awk, such as -F -v etc.; scripts is its processing script, including mode pattern and action action (the relationship between mode and action is generally that mode is responsible for determining valid fields, and action is responsible for processing them)
1. Simple use of print
Create a simple test The file is as follows:
[root@mos download]# cat demo.txt
Welcome to mos blog.
This is a test file.
Example: Print the entire line: $0
[root@mos download]# awk '{print $0}' demo.txt
Welcome to mos blog.
This is a test file.
Example: Print the last field of each line: $NF
[root@mos download]# awk ' {print $NF}' demo.txt
blog.
file.
Example: Print the second field: $2
[root@mos download]# awk '{print $2}' demo.txt
to
is
Example: Print the penultimate field of each line and print OK after it
[root@mos download]# awk '{print $(NF-1),"OK"}' demo. txt
mos OK
test OK
Example: print line number
[root@mos download]# awk '{print NR,$0}' demo.txt
1 Welcome to mos blog.
2 This is a test file.
Example: Print a specific value of the current system environment variable, such as path, the output of the following two is the same
[root@mos download]# awk '{print ENVIRON["USER"];}' demo. txt
root
root
[root@mos download]# awk 'BEGIN{print ENVIRON["PATH"];}'
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local /sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PS: The following files are only for successful execution of print, BEGIN can be output before execution The data is often written as report titles and test output. Its content cannot obtain variables related to file modification operations, which will be mentioned later;
Example: The default delimiter of awk is a space, and some text words are represented by other characters. Delimiter, the following two examples are the same
[root@mos download]# awk -F: {'print $1,$NF'} /etc/passwd|tail -1
mos1 /bin/bash
[root@mos download]# awk -v FS=: '{print $1,$NF}' /etc/passwd|head -1
root /bin/bash
Example: Modify the output delimiter, special characters need to be escaped, as follows Example:
[root@mos download]# awk -v OFS=. '{print $1,$NF}' demo.txt
Welcome.blog.
This.file.
[root@mos download]# awk -v OFS="~"'{print $1,$NF}' demo.txt
Welcome~blog.
This~file.
[root@mos download]# awk -v OFS=" '{print $1,$NF}' demo.txt
Welcome"blog.
This"file.
[root@mos download]# awk -v OFS=9 '{print $1,$NF}' demo.txt
Welcome9blog.
This9file.
[root@mos download]# awk -v OFS=XXXX '{print $1,$NF}' demo.txt
WelcomeXXXXblog.
ThisXXXXfile.
PS: If in the print output field If there is no comma between them, the output separator will be invalid, and the output results will be directly connected together. There are many default variables in awk, such as those listed before: $1~$n, $0, FS, OFS, etc. The picture below shows the built-in variable table of awk:
Attribute description
$0 Current record (as a single variable)
$1~$n The nth field of the current record, the fields are separated by FS
FS The default input field separator is Space
NF The number of fields in the current record is the number of columns
NR The number of records that have been read is the line number, starting from 1
RS The input record separator defaults to the newline character
OFS output The default field separator is also a space.
ORS The output record separator, which defaults to a newline character.
ARGC The number of command line parameters.
ARGV The command line parameter array.
FILENAME The name of the current input file.
IGNORECASE If true, then Perform case-ignoring matching
ARGIND ARGV identifier of the currently processed file
CONVFMT Number conversion format %.6g
ENVIRON UNIX environment variable
ERRNO UNIX system error message
FIELDWIDTHS A whitespace-delimited string of input field widths
FNR Current record number
OFMT Number output format %.6g
RSART The beginning of the string matched by the matching function
RLENGTH The length of the string matched by the matching function
SUBSEP
Disclaimer: Since many variables are not commonly used by the author, I have not listed them all here.
For more detailed explanations on the use of Linux awk commands, please pay attention to the PHP Chinese website!