Sed mode space and reserved space

  
 

The sed editor processes the file line by line and prints the output to the screen. The sed command reads the currently processed line into the pattern space, and sed prints the processed line to the screen after executing all the commands on the line (unless the previous command deletes the line), sed After processing a line, it is deleted from the pattern space, and then the next line is read into the pattern space for processing and display. After processing the last line of the file, sed will end. Sed processes the file in a temporary buffer (mode space), so the original file will not be modified unless the -i option is indicated.

Commands related to mode space and hold space:

n Output mode space lines, read the next line to replace the current mode space, execute the next processing command instead of The first command.

N Read the next line and append it to the pattern space line. There are two lines in the pattern space.

h Copy the lines in the pattern space to the reserved space.

H Append lines from the pattern space to the reserved space.

g Replace the rows of the pattern space with the contents of the reserved space.

G Appends the contents of the reserved space to the line in the pattern space.

x Swaps the contents of the reserved space with the current line in the pattern space.

! Applies commands to all rows except the selected row.

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

The following 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

aaa line Read in the pattern space, execute G, append a blank line after this line, and then print the pattern space, the other lines are the same.

Add a blank line after the matching line:

sed ‘/ccc/G’ datafile

111111111111 aaa

222222222222 bbb

333333333333 ccc

444444444444 ddd

555555555555 eee

666666666666 fff

Add a blank line before the matching line:

sed &lsquo ;/ccc/{x;p;x;}’ datafile

111111111111 aaa

222222222222 bbb

333333333333 ccc

444444444444 ddd

555555555555 eee

666666666666 fff

Changes in reserved space and mode space before and after command execution:

Commands reserve space mode space

x Before: null After execution: ccc\ Before execution: ccc\ After execution: null

p Before execution: null After execution: ccc\ Before execution: ccc\ After execution: null Output a blank line

x Before execution: ccc\ After execution: null Before execution: null After execution: ccc\ Output ccc where line

(Note: Write ccc line as ccc)

Delete even lines:

sed ‘{n;d;}’datafile

111111111111 aaa

333333333333 ccc

555555555555 eee

After executing n The first row, the second row is read and then execute the command d, i.e. remove this line; n and then performing printing of the third row, the fourth row is read and then execute the command d, and so on.

Add a new line after the even line:

sed ‘{n;G;}’ datafile

111111111111 aaa

222222222222 bbb< Br>

333333333333 ccc

444444444444 ddd

555555555555 eee

666666666666 fff

After executing n, the first line is output to standard output, Then the second line enters the pattern space. According to the previous explanation of G, a blank line is inserted after the second line, and then output; then n is executed to output the third line to the standard output, and then the fourth line enters the pattern space. And insert a blank line, and so on.

Corresponding: sed ‘{n;n;G;}’ datafile means to insert a blank line after the 3, 6, 9, 12, … line of the file.

Empty even lines:

sed ‘{n;g;}’ datafile

111111111111 aaa

333333333333 ccc

555555555555 eee

Print the first line after executing n, then read the second line to execute the g command. The g command replaces the current mode space with the reserved space content (null), ie the second line is blanked. . Other lines and so on.

Merge even lines to the previous line:

sed ‘{N;s/\ /\\t/;}’datafile

111111111111 aaa 222222222222 bbb

333333333333 ccc 444444444444 ddd

555555555555 eee 666666666666 fff

Execute N, append the second line to the first line of the pattern space, then the pattern space uses two lines, then Perform a replacement (s) to replace the first newline with a tab. Other lines and so on.

Add a line number, roughly equivalent to cat -n datafile:

sed = datafile

1

111111111111 aaa

2< Br>

222222222222 bbb

3

333333333333 ccc

4

444444444444 ddd

5

555555555555 eee

6

666666666666 fff

sed = datafile

Copyright © Windows knowledge All Rights Reserved