[Concept introduction]: awk is a language that can process data and generate formatted reports , The function is very powerful. Awk thinks that each line in the file is a record. The separator between the record and the record is a newline character,
Each column is a field The separator between a field and a field is one or more spaces or tabs by default.
awk works by reading data, treating each row of data as a record (record) Each record is divided into several fields by field separators, and then the value of each field is output. strong>
gawk-pattern scanning and processing language
mode scanning and processing language
[Function description]: Good at operating on columns, good at analyzing and processing log files
[Principle of Implementation]:
< img alt="share picture" src="/wp-content/uploads/images/os/linux/1626797203428.png" >
[Syntax Structure]:
awk parameter’matching mode {action instruction}’ file information
sed parameter ‘Matching condition action instruction’ file information
Basic operation part:
query filter
-F “”Specified column Separator (parameter)
awk uses spaces as the symbol to separate columns by default. By default, there are multiple consecutive spaces as a whole.
awk uses -F to specify the separator, between multiple spaces It also means one or more columns of information
-f file specifies the file name of the reading program -v var=value defines the use in the awk program Note: The awk program script is defined by the opening and closing braces. The script command must be placed between two curly braces. Since the awk command line assumes that the script is a single text string, you must enclose the script in single quotes.
[Built-in variables]
NR== Display specified line number information (mode)
< p>NR means line information in the file
NF means the column information in the file $NF Take out the last column $(NF-1) Take out the second-to-last column of the file
FS means designated separator information-F == FS
Field extraction: extract a column of data in a text and print it out
Field-related built-in variables
$0 means the entire line of text< /strong>
$1 represents the first data field in the text line
$2 represents the second data field in the text line
$N represents the Nth data field in the text line
$NF means the last data field in the text line
Exclude specified information
simulation replacement and modification information
< p>print gsub displays the information of the specified column (action)
< span style="font-family: 黑体; font-size: 14pt;"> awk modify and replace instructions: gsub — gawk substitute
awk replacement instruction user: gsub(/information to be replaced/,”replace with what information”,$replace the information in the first column)< /span>
The priority of awk program operation is: 1) BEGIN : Execute before starting to process the data stream, option 2)program: How to process the data stream, mandatory option 3)E ND: Execute after processing the data stream, optional
Test environment preparation:
Basic exercise questions:
01. Display Xiaoyu’s last name and ID number
Method 1: According to the file line number, display the specified content
1) Find the line with xiaoyu information
2) Display the specified column信息第一列第三列
方法二: 根据字符Information, display the specified content
1) Find the line with xiaoyu information in the second column
2) Display the specified column information in the first column and the third column
< /p>
02. People whose last name is Zhang, show his second donation amount and his name
The first journey: find out that the last name is zhang
The second course: display and output the second donation record and name
03. Display the full name and ID number of all people whose ID numbers start with 41
The first course: Find out the ID number information starting with 41
The second course: show the specified column
04. 不要显示所有以41开头ID号码的人全名和ID, 将其他人员全名和ID号码显示
05. Display the full name of all ID numbers whose last digit is 1 or 5. Display the id number
06. Show Xiaoyu’s donations, each time starts with $, such as $110$220 $330 —Replace
The first course: display the specified amount
Second course: Modify the amount representation format to use $ to represent
Exercise:
01. How to get the IP address
The first course: How to display the IP address
The second course: Display the line with the specified IP address
p>
The third course: Display IP address
< /p>
02. 如何取出文件权限数值
Professional commands to obtain file permissions
Advanced operation part: To log files Analyze and process
awk program priority
About the execution priority of awk program, BEGIN is the highest priority The code block is executed before PROGRAM is executed, and there is no need to provide a data source, because it does not involve any data processing, and does not rely on the PROGRAM code block;
What does PROGRAM do with data flow? The required code block is also the default code block. Therefore, the data source must be provided during execution;
END is the operation after processing the data stream. If the END code block needs to be executed, it must be supported by PROGRAM and cannot be executed individually.
The role of awk mode:
· Normal mode: Regular information is matched and compared to match the information range mode
· Special mode:
BEGIN: Processing Before the content of the file, complete the BEGIN operation process
Usage
awk definition array
Array definition method: array name[index]=value< /p>
Awk operation
- Assignment operation=
- Comparison operation> >= == <<= !=
- Math operation +-* /% ** ++ —
- Logical operation&& ||
- Matching operation~ !~
1. Assignment operation: mainly for variables or arrays Assignment, such as:
Variable assignment name=’baism’ school=’ayitula’
Array assignment array[0]=100
2. Comparison operation, if the comparison is a string, it will be compared according to the ascii coding order table. If the result is true, it is represented by 1, and if it is false, it is represented by 0.
3. Mathematics Operation
4. Logical operation
And operation: true true is true, true False is false, false is false
Or operation: true is true, true or false is true, false or false is false
5. Matching operation
p>