P Print Mode space content
d Delete Mode space content
A appends a row
I insert a row
C Replace Line
! Non -
N reads a row, overwriting the schema space content
For example echo-e ' 1\n2\n3\n4 ' |sed ' n;s/\n//'
The first line is read first, when the mode space is 1$
Then executes the n command, after the output mode space, reads the next line, this time the mode space is 2$, then executes after the s/\n//
The next line is then read in, and the mode space is 3$
。。。
N reads a row and appends to the pattern space
For example echo-e ' 1\n2\n3\n4 ' |sed ' n;s/\n//'
The first line is read first, when the mode space is 1$
Then executes the n command, appending to the pattern space, where the mode space is 1\n2
Then execute the s/\n//command, delete \ n, and output the schema space content 12
Next, continue reading to the next line, when the mode space is 3$
。。。
P Print Mode space content
For example echo-e ' 1\n2\n3\n4 ' |sed '/2/p '
First read the first line, when the mode space is 1$, does not match
Then read into the second line, where the pattern space is 2$, matching the Execute P command
。。。
Note: Since SED executes line by row, the mode output mode space content, so when the match executes P, the match line prints two times
P Print mode space first row
Give an example echo-e ' 1\n2\n3\n4 ' |. /sedsed-d--hide=hold ' N; P
First read into the first row, the mode space is 1$, then execute the n command, read the next line and append to the pattern space, the mode space is 1\n2, and then execute the P command print mode space the first line is \ n front of the content
Then pattern output mode space
Next, continue reading to the next line, when the mode space is 3$
。。。
d Delete Mode space content
Give an example echo-e ' 1\n2\n3\n4 ' |. /sedsed-d--hide=hold '/2/d '
First read into the first line, when the mode space is 1$, does not match the automatic output
Read the second line again, this time the mode space is 2$, match, execute D command, empty the mode space
Then read into the next line, where the mode space is 3$
。。。
Note D will not pass to standard output after execution, and will discard subsequent commands to read new content
D Delete the first line of the pattern space
Give an example echo-e ' 1\n2\n3\n4 ' |. /sedsed-d--hide=hold ' n;d '
First read into the first line, when the mode space is 1$, the execution of the n command, the mode space is 1\n2, and then execute the d command, delete the first line after the 2$, do not pass the standard output, discard the following command to read the next line
Read in the next line, this time the mode space is 2\n3 ...
H overwrites content in the current mode space to hold space
H appends the contents of the current pattern space to the hold space
G Overwrite content in current hold space to pattern space
G Append the contents of the current hold space to the mode space
x swaps the current space and schema space content
#################################
1. Delete empty lines
General situation sed '/^$/d ' file
Special case, Existence tab and other tabs sed '/^\s*$/d ' file
\s for tab \ r \ n Spaces
2. Print line numbers
Sed-n '/if/= ' file
Prints the line number of the line that matches if
Sed-n ' $= ' file
Total number of lines printed file
3. Multi-line command
N reads the next line into the pattern space, inserts the line below the current row in the pattern space and divides it by \ n, and then reads the schema space content before it is read into the schema space instead of the previous content
D Delete the first line of the mode space, the first action \ n before the content; D is to delete the schema space so the content
P The first line of the print mode space, the content before the first action, and P is the print mode space so the content
Note, the Print command is typically used after next, before the delete
H Copy the contents of the pattern space to the hold space, and the space of the pattern space, and H appends the schema spatial content to the hold space, and adds a newline character after the original hold space.
G will keep the contents of the space copied to the pattern space, covering the mode space, G will keep the space content appended to the pattern space, after the original mode space to add a newline character
X-swap mode space and preserve space content
4. Tags
b tag, unconditional jump
Sed '/lable/ba;s/lable/lable/;:a;s/$/\!/' file
If match lable jumps to label a
Mismatch lable is executed from left to right
T tag, conditional jump
Sed '/lable/s/lable/lable/;t;s/$/\!/' file
If the match executes, the match is followed by the substitution
Do not match jump to the T tag, perform the subsequent substitution
This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1749156
Sed common knowledge points