N: Reads a row, executes N, prints the current line to standard output, reads a row, overwrites the current row, and then executes a set of patterns/behaviors on the pattern space.
N: Reads a row, executes N, reads a row, and now has two lines of content in the pattern space, performing a set of patterns/behaviors.
As follows:
[[Email protected] ~]# sed ' n;p; ' Num.txt
Num1
Num2
Num2
Num3
Num4
Num4
[[Email protected] ~]# sed ' n;p; ' Num.txt
Num1
Num2
Num1
Num2
Num3
Num4
Num3
Num4
P: lowercase p, print the contents of the mode space to standard output
P: uppercase P, which prints the first line in the pattern space to the standard output
As follows:
[Email protected] ~]# sed-n ' n;p; ' Num.txt
Num1
Num2
Num3
Num4
[Email protected] ~]# sed-n ' n; P; ' Num.txt
Num1
Num3
D: Delete all contents of the pattern space, ignoring subsequent patterns/behaviors
D: Delete the first line of the pattern space, ignoring the following pattern/behavior
Delete lines 2nd, 3
[[Email protected] ~]# sed ' 2{n;d;} ' Num.txt
Num1
Num4
Delete Line 2nd
[[Email protected] ~]# sed ' 2{n;d;} ' Num.txt
Num1
Num3
Num4
Note: sed ' 2{n;d;} ' num.txt is equivalent to sed ' 2{n;d;p} ' num.txt because the p after D is ignored and will not be executed. Capital D is the same.
H: Overwrite the contents of the pattern space with the reserved space
H: Append the contents of the pattern space to the reserved space
1, 2, 3 rows are appended to the reserved space, for the third row, Exchange back, note: The reserved space initial value is a blank line
[[Email protected] ~]# sed ' h;3{x} ' num.txt
Num1
Num2
Num1
Num2
Num3
Num4
How to remove the empty line above, the first line to overwrite
[[Email protected] ~]# sed ' 1h;1! H;3{X} ' Num.txt
Num1
Num2
Num1
Num2
Num3
Num4
G: Overwrite the contents of the reserved space with the pattern space
G: Append the contents of the reserved space to the pattern space
1, 2, 3 rows are appended to the reserved space, and for the third row, cover back
[[Email protected] ~]# sed ' h;3{g} ' num.txt
Num1
Num2
Num1
Num2
Num3
Num4
1, 2, 3 rows are appended to the reserved space, and for the third row, append back
[[Email protected] ~]# sed ' h;3{g} ' num.txt
Num1
Num2
Num3
Num1
Num2
Num3
Num4
Common commands for SED