Basic syntax:
The basic format of the awk instruction is: awk [parameter] & lsquo; awk program & rsquo; file name
The main structure of the awk program is: pattern {action}< Br>
Generally use “relational expressions" as a pattern, for example: $1>4, $2==$3,"banana"~/an/(
This is a regular Matching judgment relationship, if A is a string, B is a positive expression, A~B (determine whether string A contains a substring that can match B expression); A!~B (determine whether string A is Does not contain substrings that match B expressions))
action mainly includes some instructions, such as awk's i/o instructions: print, printf, getline; awk's flow control instructions: if(... ){...}else{...}, do{...}while();
The awk program sequentially searches for each pattern for each input line, for each match. The above mode executes the corresponding action, then reads the next line and starts matching again, knowing that all the input has been processed.
A pattern or action can be omitted in a statement. The default pattern is to match all lines. The default action is to output the current line. Awk reads one line (one record) from the input at a time, and the default line separator is \ . Then awk separates the records into fields, and the default fields are separated into blanks. The first field in a row is $1, followed by $2, $3..., and the entire record is $0.
Awk's built-in variables can be used to get relevant information. Common ones are:
NF(Number of Fields): an integer whose value represents the field contained in $0. number.
NR (Number of Records): is an integer whose value indicates the number of data lines that awk has read.
FILENAME: The name of the data file being processed.
For more information, please refer to http://www.jiayii.com/awk-tutorial/
Basic operation example: test file test.txt, the content is as follows:
(Please note that the blanks may be blank or tab)
1. To get all of John's transcripts, use these commands:
awk '/John/' test.txt (action defaults to output the entire line)
awk '/John/{print $0}' Test.txt
awk '$2=="John"{print $0}' test.txt (default is separated by white space, $2 represents the second field of each line)
Awk '$2~/John/{print $0}' test.txt ($2 and regular expression /John/match)
2. Get a transcript with a score greater than 120
awk '$3>=120' test.txt
3. If the original data file is separated by “, & rdquo;, then all statements can add separator parameters, such as: awk -F", " /John/' test.txt (this is the role of -F)
BEGIN and END
Two special forms in awk that can be used in pattern, providing BEGIN and END to give the program an initial state and execute after the program ends Some finishing work. Any operation listed after BEGIN will be executed before awk starts scanning input, and the operations listed after END will be executed after scanning the full input. Therefore, BEGIN is usually used to display variables and preset initial values, and END is used to output the final result.
Or the above example:
1. Add all of John's scores. The available commands are as follows:
2. The default initial score is 60, and then the scores in the file are accumulated:
Awk array using
aw array using strings as arrays Standard; does not need to be initialized when used;
The actual application array is also the most widely used, we still use test.txt as the data file to calculate the total score of each member:
Practical application
When I use awk, I often get some statistics from the online, and often these log files are not very regular when used, then you can do it first, through grep Or other commands convert the file into a regular output and then use awk for processing.
Today, I have brought the steps of adding static routes for Linux, I hope to help my friends! 1. Add
Today we will learn how to find and remove duplicate files on a Linux PC or server. Here is a tool
Matthew Garrett, Core Developer from Red Hat, publishes a detailed article on Fedora 18. It will int
Enterprise customers install SCOM2012 behind the constantly generated alarms, performance, informat
Novice to see: Linux system startup time limit optimization
Get the current system time with C under Linux
Using Beowulf to Transform Common PCs into Clusters
Neighbor subsystem of linux protocol stack (related process 3)
Several ways to run linux process background
U disk installation Linux skills
Microsoft will pre-install Skype
Analysis of differences between Unix and Linux operating systems
Linux operating system firewall configuration modification instruction set
Solve the boot problem of Linux and Windows dual system
How does Windows 7 disable the disk defragmentation service?
Win10 application store page blank error 0X80072EE2 how to solve?
What is the win7 shutdown shortcut?
Remote Desktop Connections exceeded
Win8.1 Windows shortcut key combination shortcuts Daquan
QQ Hyun Dance September 10th Ocean Wonderful Shocking Attack Website
Four reasons to look at Win8's most attractive student users