Sed Structure Analysis + awk Structure Analysis

Source: Internet
Author: User
Tags array definition line editor

Sed Structure Analysis

Sed/awk provides many tutorials, most of which are examples of teaching materials, or grammar textbooks.

After learning SED and awk, the results are learned, which is easy to confuse and the syntax is easy to confuse.

This article mainly explains the structure of SED, so that you can have a holistic grasp of SED after learning.

As for what is the structure, you can understand it after reading it.

I have learned a limited number of SED/awk courses. Please do not hesitate to point out the errors.

1: sed syntax

Sed-e 'pattern' + action' textfile

Meaning: for a text file textfile, use pattern to match, find the matched string, copy it to pattern space, and then execute action on the text in pattern space.

After the action is executed, start from the next row of the current row of textfile, continue to use pattern to match, continue to copy to pattern space, continue to execute the action until the end.

Explanation 1: Because the matched string is copied to pattern space, action is a string operation in pattern space, so it does not affect the original file.

Detailed process:

** Sed execution process:

A. If it has reached the end of the file, Goto E; otherwise, use pattern to match, find the matched string, copy it to pattern space, and then execute action on the text in pattern space.

B. Execute the command sequence. The command is only valid for the mode space;

C. Add 1 to the current row number

D. Goto;

E. Exit

2: Pattern -- Combination of rows (basic unit) and string matching (basic action)

Sed is called a line editor. When sed sees a text, in its mind, this text is a line separated by a line break until infinite lines.

In SED's eyes, there is only one basic unit of text, that is, rows. These rows are numbered 1, 2, 3, 4, 5, 6,... $ respectively.

The row has only one action, that is, the row matches a string sequence or does not match a string sequence.

Combined, there are the following pattern types.

A. Row A does something sed-e '3d 'textfile

B. Rows A to B Do Something sed-e '3, 5d 'textfile

C. Match the sed-e '/www/d' textfile line of a string Sequence

D. Match the character string "" to "" many lines of text sed-e '//,//P' textfile

E. Match the text sed-e '3 from line A to "This salute",/This salute/P' textfile

F. match a text section starting with "hello" and ending with line B sed-e '/Hello/, 10p' textfile

3: Action

There are many common actions and related documents. You can search for them online.

It must be noted that

A. Hold space and pattern space.

Copy the matched string to pattern space and then execute the action on pattern space.

If we have to deal with some other things before processing the content in this pattern space, that is, there are two or more operation objects, then a buffer is obviously not enough.

Therefore, the hold space is introduced.

B. S/Regexp/replacement/can be viewed as/Regexp/S/Regexp/replacement/

Well, the rest is to understand all sed actions, so that you can use sed freely.

Awk Structure Analysis

1: awk syntax

Gawk 'pattern{ action} 'textfile

At first glance, it is similar to SED's 'patternaction' textfile syntax.

This is one of the reasons why SED/awk is confusing. The SED/awk syntax is similar. The slight difference is that the awk action must be enclosed in braces.

The execution process of awk is also. Input text, match pattern, execute action, then match pattern, and then execute action. Note: awk has no mode space.

Its actions are also different from those of sed.

2: Pattern

In awk's eyes, a text is seen as a table.

In details, awk uses the RS string to cut the data into one row regardless of the text, and then uses FS to cut each row into one table item.

Therefore, the text in awk has two basic units: rows and table items. (Recall that SED text has only one basic unit, line ).

Each row in the awk is numbered 1, 2, 3, 4, 5,... nr.

The number of each table item in each row is $1 $2 $3... $ NF. The special number for the entire row is $0.

A. Since Nr NF is a number and $ n can also be a number, you can compare the numbers to find the pattern.

For example, if the value of the third item is the date number, you can write $3> 20 if the judgment date is greater than 20.

There are 6 methods to compare numbers:

<=

>>=

=! =

B. Because $ n can also be a string, you can also use string matching to construct pattern.

For example, the row mode containing the string "www" can be written as $0 ~ /Www/

For example, the fifth item does not contain the string "com", and the mode can be written as $5 !~ /COM/

C. The mode can be in the mode range.

A pattern range is two patterns separated by a comma.

The action is saved Med for each input line between the occurrence of the first and second pattern.

/Regular expression 1/,/regular expression 2/{print $0}

D. Mode Combination

Patterns can be combined to provide more powerful and complex matching.

The following symbols are used to combine patterns.

| Logical or, either pattern can match

& Logical and, both patterns must match

! Logical not, patterns not matching

3: Action

Action is to operate on the selected object.

The main actions are as follows.

Action 1: print, printf, and printf are basically similar to C.

Action 2: built-in functions. The most common one is gsub/Sub.

Add the C language for, while, do-while, if-else process control,

Coupled with the variables featured by awk, the array definition and use constitute the awk action.

4: Begin/end

Set some FS, RS, and other actions before begin.

End {} executes some actions at the end.

This is the general structure of the awk, which requires a special understanding of the awk array, understanding the usage of the built-in functions of the awk, and repeating the exercises, you should be able to freely apply the awk

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.