Linux_ three sword passengers _awk

[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.

      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

share picture

  -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:

Share pictures

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
Share a picture
2) Display the specified column信息第一列第三列
分享图片

方法二: 根据字符Information, display the specified content
1) Find the line with xiaoyu information in the second column
Share a picture
2) Display the specified column information in the first column and the third column
share picture< /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
Share a picture
The second course: display and output the second donation record and name
share picture

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
Share a picture
The second course: show the specified column
share picture

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
 Share picture

06. Show Xiaoyu’s donations, each time starts with $, such as $110$220 $330 —Replace
The first course: display the specified amount
share picture

Second course: Modify the amount representation format to use $ to represent
Share a picture

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

Share a picture< /p>

02. 如何取出文件权限数值
分享图片

Professional commands to obtain file permissions
share pictures

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
share picture

Usage

awk definition array

Array definition method: array name[index]=value< /p>

share picture

Awk operation

  1. Assignment operation=
  2. Comparison operation> >= == <<= !=
  3. Math operation +-* /% ** ++ —
  4. Logical operation&& ||
  5. Matching operation~ !~

1. Assignment operation: mainly for variables or arrays Assignment, such as:

Variable assignment name=’baism’ school=’ayitula’

Array assignment array[0]=100

 Share picture

share picture

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.

Share a picture

share picture

3. Mathematics Operation

share picture

share Picture

4. Logical operation

And operation: true true is true, true False is false, false is false

share picture

Or operation: true is true, true or false is true, false or false is false

share picture

5. Matching operation

Share a picture

share picture

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5088 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.