Linux awk-general Linux technology-Linux programming and kernel information. The following is a detailed description. Awk is a very useful tool in linux for processing files and strings. It also provides built-in functions, so it is also a programming language. Tools that play a similar role include sed and grep. I will keep learning awk these days.
Here I only talk about the awk command and won't involve the awk script. In fact, the two are the same.
Each time a row of the file is read, each line is separated into several fields by the domain separator, with $1, $2, $3 ,... $9, & (10 )... indicates the first domain and the second domain ...., $0 indicates all fields in a row. By default, space is used as the domain separator, which is also a common method. Therefore,-f domain separators are generally not used.
Like sed and grep, awk reads the file content to the cache, operates on it, and then outputs the result to the screen without modifying the source file, to modify the source file, use the following format:
Awk comand filename | tee filename
The mode consists of some selection statements (which contain regular expressions to match specific rows), which indicate when the action is triggered. The mode has two special fields, BEGIN and END. BEGIN indicates the header, which is executed before the browsing line, END indicates the END, and executed after all browsing. The default mode is to print all rows.
The action is usually {print $1...}, which is to print some fields of each row.
The following are some examples:
$ Cat file
1 nm hello 2000
2 jack good 2001
3 eric happy 2002
$ Awk '{print & 0}' file
Result:
1 nm hello 2000
2 jack good 2001
3 eric happy 2002
$ Awk '{print $1, $3, $4}' file
Result:
1 "hello 2000"
2 good 2001
3 happy 2002
$ Awk 'in in {print "BEGIN test"} {print & 0} END {print "end test"} 'file
Result:
Begin test
1 nm hello 2000
2 jack good 2001
3 eric happy 2002
End test
$ Awk '{if ($2 = "nm") {print $0}' file
$ Awk '$2 = "nm" {print $0}' file # The two commands have the same results. We recommend that you use the second statement.
1 nm hello 2000
$ Awk '$3 ~ /O/{print $0} 'file
Result:
1 nm hello 2000
2 jack good 2001
$ Awk '$3 ~ /^ H/'file
Result:
1 nm hello 2000
3 eric happy 2002
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service