Gawk notes (2)

Source: Internet
Author: User

> Pattern refinement

First, let's talk about the execution sequence of multiple modes between in and end. I wrote the followingProgramTo verify:

Figure 1 sequential verification of multiple modes (the data file is cars)

For example, begin and end have three modes. The default mode determines the factory time ($3> 2000) and brand ($1 ~ /Ford/). The running result is as follows:

Figure 2 running result

It can be seen that multiple gawk files match one record in sequence.

· Syntax and operator used in pattern:

1. Regular Expression

As shown in figure 1, enclose the regular expression with // to use this expression as the mode:

A separate "/Ford/" indicates that this expression is used to match the entire record.

You can also use "~ ", Such as" $1 ~ /Ford/"indicates that the specified field is used as the matching object. The matching object can be a field or a variable.

"! ~ "Indicates that the matching is not met.

2. Relational operators

You can use <, <=, = ,! =, >=,> And other Relational operators for numerical value and string comparison

3. boolean operators

You can use | (OR) and & (and) to combine any mode.

·Special symbols

,(Comma)

Range OperatorsIf a gawk program line is separated by commas,

Gawk selects text lines starting from 1st rows matching 1st modes.

The last line selected by gawk is the text next to the line that matches the 2nd pattern. If there is no match for 2nd text lines, gawk Selects all text lines that know the end of the input.

After gawk finds the 2nd modes, it searches for the 1st modes again to start the process again.

Eg.

Figure 3 cars Data File

Figure 4 comma operator Example 1

Figure 5 comma operator Example 2

See example 1. "awk"/Volvo/,/BMW/'cars ", first match/Volvo/, starting from line 1, and then match/BMW /, to 7th rows. Then, start matching/Volvo/again. If no match is found, it will end at the end of the file.

Example 2: "awk '/Chevy/,/Ford/'cars", match/Chevy/first, start from the first line, match successfully, then match/Ford /, the first group obtains 2 and 3 rows of data, and starts from/Chevy/. Although 5th of the behavior in cars is Ford, it matches/Chevy/, so it is skipped. It can be seen that two matching strings separated by commas are sequential.

> Special symbols

Single quote'

If the gawk program created on the command line contains spaces or special shell characters, the program must be enclosed in single quotes.

When gawk is written in the shell file:

Single quotes are used to obtain variable values from shell.

Figure 6 single quotes

Double quotation marks"

If double quotation marks are used, gawk compares the text, that is, the ASCII sorting sequence is used as the basis for comparison.

Figure 7 double quotation marks

For numeric comparison, double quotation marks are not required.

Figure 8 Comparison of Values

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.