Let's take a look at the SED mode space and storage space + arrangement)

Source: Internet
Author: User

Several examples show the mode space and the reserved space of sed.
Example 1
Sed g outputs a blank line under each line of the document
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 line of the document. Therefore, an empty line is added to the end of each line.
Extended:
Sed '/^ $/d; G'
Output a blank line under each non-empty line in the document
Sed '/^ $/d; G; G'
Output two blank lines under each non-empty line in the document
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 ^ $ in front cannot match such rows.
Sed '/[[: Space:]/d; G' is sed'/^ * $/D' for KSh'

Example 2
Sed '/RegEx/{X; P; X ;}'
Insert an empty row before any row matching RegEx
Code:
$ Cat foo
11111111111111
22222222222222
Test33333333333
44444444444444
55555555555555

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

Test33333333333
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.
Patternspace)
X: before execution: NULL after execution: test... $ before execution: test... $ after execution: NULL
Before p execution: After null execution: test... $ before execution: test... $ after execution: NULL outputs an empty row
X before execution: test... $ after execution: before execution of null: after execution of null: test... $
I personally think the above command execution analysis is a bit problematic. My analysis is as follows:
Patternspace)
X before execution: test... $ after execution: before execution of null: after execution of null: test... $
P: before execution: after execution of null: before execution of null but before execution of an empty row: test... $ after execution: test... $
X: before execution: NULL after execution: test... $ before execution: test... $ after execution: NULL
Extended: You can test sed '/test/{X; P;}' Foo or SED '/test/{P; X;}' Foo and so on to see the results, experience the changes in two spaces

Corresponding:
Sed '/RegEx/G' outputs an empty row under any row matching RegEx
Sed '/RegEx/{X; P; X; G;}' outputs an empty row before and below any row matching RegEx

Example 3
Sed 'n'; G; 'inserts an empty row under the even row of the document
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, then output; then execute n to output the third row to the standard output, and then the fourth row enters the mode space, and insert a blank row, and so on ....
Corresponding:
Sed 'n'; n; G' indicates to insert an empty line behind the 3, 6, 9, 12,... line of the document
Sed 'n'; n; G' indicates to insert an empty line behind the 4, 8, 12, 16,... line of the document
Sed 'n'; D' indicates that the document is deleted from an even number of rows.

Example 4
Sed '$! N; $! D' output the last two lines of the document, 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 document, after N puts the next row of the current row into the mode space, d deletes the content of the mode space: D is to delete the first line of the pattern space); (when the loop is deleted to the pattern space, only the last and second rows are left) to the last and second rows, append the last line to the bottom of the second line, and then the last line does not execute D, so the last two lines of the document are saved.
There is another usage code of N:
$ Sed = Foo | sed N (this command format is required)
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 to format the output document.

Example 5
Sed '1! G; h; $! D'
Sed-n' 1! G; h; $ P'
Display the lines in reverse order of the document, 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 mode space to the hold 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 look at sed '1! G; h; $! D' maintain changes in space and mode space during command execution:
Command to keep space
First line h; d before execution: NULL after execution: 11... $ before execution: 11... $ after execution: NULL
Row 2G; h; d before execution: 11... $ after execution: 22... $/n11... $ before execution: 22... $ after execution: NULL
The second line g; h before execution: 22 .. $/n11 .. $ after execution: 33 .. $/n22 .. $/n11 .. $ before execution: 33 .. $ after execution: 33 .. $/n22 .. $/n11 .. $
This output is the reverse order of the document.

Question: The command to display a document in reverse order in VI is: G /. /M0 indicates that each line is found in the normal order of the document, and the line is placed in the top line of the document. In this way, the lines in the document are displayed in reverse order.

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.