The difference between SED mode space and staging space _linux shell

Source: Internet
Author: User
Tags first row

The SED editor processes the file line by row and prints the output to the screen. The SED command reads the currently processed rows into the mode space (pattern spaces) for processing. When SED finishes all the commands on the line and prints the processed rows to the screen (unless the previous command deletes the row), the SED processes the row and removes it from the mode space. The next line is then read into the mode space for processing and display. When the last line of the file is processed, the SED is finished. SED processes the file in a temporary buffer (pattern space), so it does not modify the original file unless it displays the-I option.

Commands related to pattern space and staging space (hold spaces):

n Output mode space row, read the next line to replace the current mode space, and execute the next processing command instead of the first command.
N reads the next line, appends to the pattern space line, where the mode space has two lines.
H copies the lines in the mode space to the staging space.
H the line in the pattern space is appended to the staging space.
G replaces the line of the pattern space with the contents of the staging space.
G appends the contents of the staging space to the line of the pattern space.
X swaps the contents of the staging space with the current line in the pattern space.
! Applies a command to all rows except the selected row.

Note: A blank line is stored by default in the staging space.

Here are some examples:

Cat DataFile
111111111111 AAA
222222222222 BBB
333333333333 CCC
444444444444 DDD
555555555555 Eee
666666666666 FFF

Add a blank line after each line:

Sed ' G ' datafile
111111111111 AAA

222222222222 BBB

333333333333 CCC

444444444444 DDD

555555555555 Eee

666666666666 FFF

The AAA line is read into the mode space, executes G, appends a blank line after the row, and then prints the mode space, and the other lines are the same.

Add a blank line after the matching row:

Sed '/ccc/g ' datafile
111111111111 AAA
222222222222 BBB
333333333333 CCC

444444444444 DDD
555555555555 Eee
666666666666 FFF

Add a blank line before matching rows:

Sed '/ccc/{x;p;x;} ' datafile
111111111111 AAA
222222222222 BBB

333333333333 CCC
444444444444 DDD
555555555555 Eee
666666666666 FFF

Changes in staging space and mode space before and after the command execution:

Command staging space mode space
X before: null execution: ccc\n before execution: ccc\n: null
P Before: null execution: ccc\n before execution: ccc\n after execution: null output a blank line
X before execution: ccc\n: null before: null execution: ccc\n output CCC

(Note: The CCC line is abbreviated to CCC)

Delete even rows:

Sed ' {n;d;} ' datafile
111111111111 AAA
333333333333 CCC
555555555555 Eee

Print the first line after N, and then read the second line to execute the d command, delete this line, then print the third row in N, then read into line fourth to execute the D command, and so on.

Add a new row after an even row:

Sed ' {n; G;} ' DataFile
111111111111 AAA
222222222222 BBB

333333333333 CCC
444444444444 DDD

555555555555 Eee
666666666666 FFF

After the first line is output to the standard output after n execution, then the second line enters the mode space, which, according to the previous face G explanation, inserts a blank line after the second line, then outputs, then executes n to output the third row to the standard output, and then line fourth enters the mode space, inserts a blank row, and so on.
Accordingly: sed ' {n;n; G;} ' DataFile represents the 3,6,9,12,... in the file Inserts a blank line after the line.

To empty even-numbered lines:

Sed ' {n;g;} ' datafile
111111111111 AAA

333333333333 CCC

555555555555 Eee

After performing n, print the first line, then read the second line to execute the G command, and the G command replaces the current mode space with the contents of the staging space (null), that is, the second row is empty. Other rows and so forth.

Combine even rows to the previous line:

Sed ' {n;s/\n/\t/;} ' datafile
111111111111 AAA 222222222222 BBB
333333333333 CCC 444444444444 DDD
555555555555 Eee 666666666666 FFF

Executes N, appending the second row to the first row of the pattern space, with two rows in the mode space, and substituting (s) to replace the first newline character with tab. Other rows and so forth.

The line number, roughly equivalent to cat-n datafile:

sed = datafile
1
111111111111 AAA
2
222222222222 BBB
3
333333333333 CCC
4
444444444444 DDD
5
555555555555 Eee
6
666666666666 FFF

sed = datafile |sed ' {n;s/\n/\t/;} '
1 111111111111 AAA
2 222222222222 BBB
3 333333333333 CCC
4 444444444444 DDD
5 555555555555 Eee
6 666666666666 FFF

Output file last 2 lines, equivalent to Tail-2 datafile

Sed ' {$! n;$!d;} ' DataFile
555555555555 Eee
666666666666 FFF

Sed ' {$! n;$!d;} ' : For previous rows in the second line of the file, N appends the next line of the current row to the pattern space, and D deletes the contents of the mode space; to the second line, append the last line to the penultimate line, and then the last line does not execute D (! The last two lines of the file are saved for the selected line-this is the last line, and the line executing the command.

Displays the line of the file in reverse order, equivalent to the TAC command:

Sed ' {1! g;h;$!d;} ' DataFile
666666666666 FFF
555555555555 Eee
444444444444 DDD
333333333333 CCC
222222222222 BBB
111111111111 AAA

1! G indicates that all but the first row executes the G command, and $!d that the remaining lines execute the D command in addition to the last line.

Look at sed ' {1! g;h;$!d;} ' Changes in the staging space and pattern space during command execution:

Handling line command staging space mode space


First line h;d before execution: null execution: aaa\n: aaa\n after execution: null


Second line g;h;d before execution: AAA execution: bbb\n1111\n before execution: bbb\n after execution: null


Last line g;h before execution: eee\n...aaa\n: fff\n...bbb\n\aaa\n before execution: eee\n after execution: fff\n...bbb\n\aaa\n

(note: shorthand for each line)

Related Article

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.