Mode space and temporary storage space of SED Editor

Source: Internet
Author: User

From: http://leowzy.iteye.com/blog/1453421


Sed can edit or modify the text in the unit of action because it uses two spaces: one is the active pattern space )", the other is the use of the two temporary buffer zones (holdingspace) that play an auxiliary role.

 

 

Sed editor processes files row by row and prints the output to the screen. The SED command reads the currently processed rows into the pattern space for processing, sed prints the processed lines to the screen after executing all the commands on this line (unless the previous command deletes this line ), after processing a row, sed deletes it from the mode space, and then reads the next row into the mode space for processing and display. After processing the last row of the file, sed stops running. Sed processes the file in the temporary buffer (mode space), so the original file is not modified unless the-I option is specified.

Commands related to the mode space and hold space:

N Space row in output mode. Read the row in the next line to replace the space in current mode and run the next processing command instead of the first command.
N reads the next row and appends it to the end of the row in the mode space. There are two rows in the mode space.
H. Copy the rows in the mode space to the temporary storage space.
H. append the rows in the mode space to the temporary storage space.
G. Replace the row of the mode space with the content of the saved space.
G. append the content of the temporary space to the row of the mode space.
X swap the content of the temporary storage space with the current row in the mode space.
! Apply commands to all rows other than the selected row.

Note: An empty row is stored in the temporary storage space by default.

The following are some examples:

Cat datafile
111111111111 aaa
222222222222 bbb
333333333333 ccc
444444444444 ddd
555555555555 eee
666666666666 fff

Add an empty line to the end of each line:

Sed 'G' datafile
111111111111 aaa

222222222222 bbb

333333333333 ccc

444444444444 ddd

555555555555 eee

666666666666 fff

The AAA row is read into the mode space, execute g, append an empty row to the end of the row, and then print the mode space. The same applies to other rows.

Add an empty row after the matching row:

Sed '/CCC/G' datafile
111111111111 aaa
222222222222 bbb
333333333333 ccc

444444444444 ddd
555555555555 eee
666666666666 fff

Add a blank row before the matching row:

Sed '/CCC/{X; P; X;}' datafile
111111111111 aaa
222222222222 bbb

333333333333 ccc
444444444444 ddd
555555555555 eee
666666666666 fff

Changes in the temporary storage space and mode space before and after command execution:

Command temporary storage space
X: before execution: after execution of null: before execution of CCC \ n: after execution of CCC \ n: NULL
Before p execution: After null execution: Before CCC \ n execution: After CCC \ n execution: Null Output a blank line
Before Execution of X: CCC \ n after execution: before execution of null: after execution of null: CCC \ N output CCC row

(Note: abbreviated CCC as CCC)

Delete an even number of rows:

Sed '{n; D;}' datafile
111111111111 aaa
333333333333 ccc
555555555555 eee

Run N and then print the first line. Then, read the second line and run the D command to delete the row. Then, run n to print the third line. Then read the fourth line and run the D command.

Add a new row after an even number of rows:

Sed '{n; G;}' datafile
111111111111 aaa
222222222222 bbb

333333333333 ccc
444444444444 ddd

555555555555 eee
666666666666 fff

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, and then the fourth row enters the mode space, insert a blank row, and so on.
Correspondingly: sed '{n; G;}' datafile indicates the number ,... Insert a blank row after the row.

Leave the even rows blank:

Sed '{n; G;}' datafile
111111111111 aaa

333333333333 ccc

555555555555 eee

Run N and then print the first line. Then read the second line and execute the G command. Use the content of the temporary storage space (null) to replace the current mode space, that is, the second line is left empty. Other rows are pushed accordingly.

Merge even rows into the previous row:

Sed '{n; S/\ n/\ t/;}' datafile
111111111111 AAA 222222222222 bbb
333333333333 CCC 444444444444 ddd
555555555555 EEE 666666666666 fff

Run n to append the second row to the first row of the mode space. Then run Replace (s) to replace the first line feed with a tab. Other rows are pushed accordingly.

Add a row number, which is 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

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

Sed '{$! N; $! D;} 'datafile
555555555555 eee
666666666666 fff

Sed '{$! N; $! D;} ': For the row before the last row of the file, after N appends the next row of the current row to 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 (! For the selected line-this is the last line, and the command is executed on other lines), so the last two lines of the file are saved.

Display the lines in reverse order of the file, which is 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 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;} 'changes in temporary storage space and mode space during command execution:

Temporary storage space for processing line commands
The first line is H; d before execution: NULL after execution: AAA \ n before execution: AAA \ n after execution: NULL
Row 2G; h; D: AAA: BBB \ n1111 \ n: BBB \ n: NULL
Last line g; before H execution: EEE \ n... After AAA \ n is executed: fff \ n... Bbb \ n \ AAA \ n before execution: EEE \ n after execution: fff \ n... Bbb \ n \ AAA \ n

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.