Common commands for SED

Source: Internet
Author: User

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

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.