Several examples show the mode space and retention space of sed.

Source: Internet
Author: User

This is an original post I posted on chinaunix for a long time ago. Later, it was quite excellent and always a bit smug.

Several examples of SED, which are not very common but useful recently, are related to hold space and pattern space. Therefore, I have explained several examples that I think are correct, post it and share it with you. Correct it and continue the discussion.

Example 1

Sed g

Output a blank line under each row of the file

Code:

$ Cat foo

11111111111111

22222222222222

33333333333333

44444444444444

55555555555555

$ Sed g foo

11111111111111

22222222222222

33333333333333

44444444444444

55555555555555



Explanation:

Usage of G in SED

The G Function appends the contents of the holding area to the contents of the pattern space. The former and new contents are separated by a newline. the maximum number of addresses is two.

Hold space: the reserved space (or the reserved space or buffer zone) is empty.

Pattern space: Pattern Space

In the preceding example, the empty hold space is appended to the end of each row of the file. Therefore, an empty row is added to the end of each row.

Extended:

Sed '/^ $/d; G'

Output a blank line under each non-empty line of the file

Sed '/^ $/d; G; G'

Output two empty rows under each non-empty row of the file

Code:

$ Cat foo

11111111111111

22222222222222

33333333333333

44444444444444

55555555555555

$ Sed '/^ $/d; G' foo

11111111111111

22222222222222

33333333333333

44444444444444

55555555555555

Note: sometimes there are some blank lines composed of space characters or tabs. The regular expression above^ $The Rows do not match.

Sed '/[[: Space:]/d; G'

Example 2

Sed '/RegEx/{X; P; X ;}'

Insert an empty row before all rows matching RegEx

Code:

$ Cat foo

11111111111111

22222222222222

Test333333333333

44444444444444

55555555555555

$ Sed '/test/{X; P; X;}' foo

11111111111111

22222222222222

Test333333333333

44444444444444

55555555555555

Explanation:

Usage of X in SED

The exchange function interchanges the contents of the pattern space and the holding area. The maximum number of addresses is two.

That is, the contents of the hold space and pattern space are exchanged.

In SED, P is used to copy the mode space to the standard output.

Analyze the content that maintains space and mode space during command execution.

Command to keep space

X: before execution: NULL after execution: Test \ n before execution: Test \ n after execution: NULL

Before p execution: After null execution: Before test \ n execution: After test \ n execution: Null Output a blank line

X: before execution: Test \ n after execution: before execution of null: after execution of null: Test \ n

(Note: The line where test is located is abbreviated as test)

Extended:

You can test sed '/test/{X; P;}' Foo or SED '/test/{P; X;}' Foo to see the results and see the changes in the two spaces.

Corresponding:

Sed '/RegEx/G'Output an empty row under all rows matching RegEx

Sed '/RegEx/{X; P; X; G ;}'Is to output an empty row before and below all rows matching RegEx

Example 3

Sed 'n'; G ;'

Insert an empty row under the even row of the file

Code:

$ Cat foo

11111111111111

22222222222222

33333333333333

44444444444444

55555555555555

$ Sed 'n'; G; 'foo

11111111111111

22222222222222

33333333333333

44444444444444

55555555555555



Explanation:

N usage in sed: Copies the pattern space to the standard output. Replace the mode space with the next line.

After N is executed, the first row is output to the standard output, and the second row enters the mode space. According to the explanation of G, an empty row is inserted after the second row, and then output; then execute n to output the third row to the standard output, then the fourth row enters the mode space, and insert a blank row.

Corresponding:

Sed 'n'; n; G'Insert an empty line after the 3, 6, 9, 12,... line of the file

Sed 'n'; n; G'Insert an empty line after the Row 4, 8, 12, 16,... of the file

Sed 'n'; D'Deletes an even number of rows of a file.

Example 4

Sed '$! N; $! D'

The last two lines of the output file, which is equivalent to tail-2 Foo.

Code:

$ Cat foo

11111111111111

22222222222222

33333333333333

44444444444444

55555555555555

$ Sed '$! N; $! D 'foo

44444444444444

55555555555555

Explanation:

D. Delete the first newline letter \ n in the mode space.

N: add the next line to the mode space.

Sed '$! N; $! D': For the row before the last row of the file, after N puts the next row of the current row into the mode space, d deletes the content of the mode space; to the second to last line, append the last line to the second to last line, and then the last line does not execute D. Therefore, the last two lines of the file are saved.

There is another usage of N.

Code:

$ Sed = Foo | sed n

1

11111111111111

2

22222222222222

3

33333333333333

4

44444444444444

5

55555555555555

$ Sed = Foo | sed 'n'; S // \ N //'

1 11111111111111

2 22222222222222

3 33333333333333

4 44444444444444

5 55555555555555



Explanation:

N is used to add a row number and format the output file.

Example 5

Sed '1! G; h; $! D'

Sed-n' 1! G; h; $ P'

Display the lines in reverse order of the file, which is equivalent to the TAC command (this command is not available on some platforms)

Code:

$ Cat foo

11111111111111

22222222222222

33333333333333

$ Sed '1! G; h; $! D 'foo

33333333333333

22222222222222

11111111111111

$ Sed-n' 1! G; h; $ P 'foo

33333333333333

22222222222222

11111111111111

Explanation:

H usage in sed: H

The H (hold) function copies the contents of the pattern space into a holding area, destroying any previous contents of the holding area.

This means to save the content of the pattern space to the reserved space.

D In sed indicates that the mode space is deleted.

1! G indicates that all the other lines except the first line execute the G command; $! D indicates that all the other lines except the last line execute the D command.

Take a lookSed '1! G; h; $! D'The space and mode space changes during command execution:

Command to keep space

First line h; d before execution: NULL after execution: 1111 \ n before execution: 1111 \ n after execution: NULL

Row 2G; h; d before execution: 1111 after execution: 2222 \ n1111 \ n before execution: 2222 \ n after execution: NULL

The second line g; h before execution: 2222 \ 1111 \ n after execution: 3333 \ n2222 \ n \ 1111 \ n before execution: 3333 \ n after execution: 3333 \ n2222 \ n \ 1111 \ n

(Note: each line is abbreviated)

In this way, the output is the reverse order of the file.

Question: The command to display a file in reverse order in VI is: G/./M0In the normal order of the file, each row is found, and the row is placed on the top of the file, so that the row is displayed in reverse order in a loop.

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.