Sed N D P

Source: Internet
Author: User

The n,d,p of SED is the command for multi-line mode space, which corresponds to N,d,p

Nth & N N (next) outputs the contents of the mode space, and then reads the new input line, the n command does not create a multiline pattern space, and N (next) creates a multiline pattern space later by reading the new input row and adding it to the existing contents of the pattern space.

Note: line breaks embedded in the pattern space can be matched with the transfer sequence "\ n" to match the first note in the space in multiline mode space, rather than the character following the line break, and the same "$" matches only the last line break of the pattern space.

D & D:d Deletes the contents of the mode space and causes the new input line to be read, and the editing method is reused at the top of the script. D removes this part of the pattern space until the first embedded line break, D does not cause the new input line to be read and it returns to the top of the script, applying these instructions to the remainder of the pattern space. For example:

Cat TESTABC sed ' /^a/{PN
P/a/Dp
}' test

Output: A
A
B

Explanation: Test is a three-line text (a,b,c); the SED matches the first letter of A's line/^a/(pattern space: a); p prints the portion of the pattern space until the first embedded newline character (a), and then appends a row in N-mode space (mode space: A\NB) P Print out the contents of the pattern space (A, B), and then if the pattern space matches to a (/a/), thed command deletes this part of the pattern space until the first embedded line break (pattern space: b), and returns the top of the script, Use these commands for the remainder of the pattern space (b), but the pattern space does not match to/^a/, so there is no subsequent output.

P & p:p Print mode space content; P Print mode space where the first embedded line break is known.

P commands are often after N and before D. N D P can establish an input/output loop to maintain the pattern space of two rows, but only one row at a time. The purpose of this loop is to output only the first row of the pattern space, then delete (D) The first line of the pattern space, and then return to the top of the script to apply all the commands to the second row of the pattern space.

Sed & awk Example: P125

Create a pattern space to match the "UNIX" at the end of the first line and the "System" at the beginning of the second line. If the UNIX system is found to span two lines, then we turn it into UNIX Operator system. Build this loop to return to the top of the script and look for "UNIX" at the end of the second line

script: sed. Print/unix$/{Ns/\nsystem/operating &/PD}
Test the script. Printhere is examples of the unixsystem. Where Unixsystem appears, it should be the unixoperating System
sed sed. Print test. Print

Output:

Here is examples of the UNIX Operating
System. Where UNIX Operating
System appears, it should be the UNIX
Operating System

The sequence of instruction execution is shown below:

The input/output loop allows us to match the Unix that appears at the end of the second line. If you normally output two lines of pattern space, you cannot match the UNIX at the end of the second line.

sed ' /unix$/{               Ns/\nsystem/operating &/p}'  test. Print output: Here is examples of the UNIX Operating System. Where Unixsystem appears, it should be the unixoperating System

It can be seen that if D is not used, the second row is appended, and after the print (p) pattern space is replaced, the third row is processed and no matching substitution is made for the second row.

So, the n command appends the next line, p prints the first line, D deletes the first line and jumps to the top of the script, and the script command acts on the remainder of the schema space; n command appends the next line, p prints the first line, and D deletes the first line ...

Sed N D P

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.