Sed advanced commands - N and D

Sed advanced command N and D

Multiline mode space

Pattern matching is line oriented, but sometimes it is difficult to match a phrase that starts at the end of one line and ends at the beginning of the next line. It can be seen that if there is too much content in the first line, a phrase at the end of the first line cannot be completely displayed in the first line, part of this phrase will be displayed in the second line, In this way, the phrase cannot be completely matched when matching. At this time, the multiline pattern space can solve this problem
The following is a simple example to explain the above point

//Originally, complete is a complete word, but because there are too many contents in the first line, it cannot be displayed completely, and some are displayed in the second line, so you can use the multi line mode space
[root@localhost ~]# cat test 
this is a test file , if this first line not display compl
ete , use next 

Append Next line

  • The multiline Next (N) command creates a multiline pattern space by reading a new input line and adding the new input line to the existing content of the pattern space.
  • The initial content of the pattern space and the new input line are separated by a newline character
  • Newline characters can be matched with escape sequence '\ n'
    The following example demonstrates N:
//Don't use N to match content
[root@localhost ~]# sed -n '/compl/p' test 
this is a test file , if this first line not display compl

//Use N to match, add the second row to the pattern space, and create a multi row pattern space
[root@localhost ~]# sed -n '/compl/N;p' test 
this is a test file , if this first line not display compl
ete , use next 

The following example demonstrates the use of N, and removes the newline between the first line and the second line to display it on one line

//After creating the multi line mode space, you can continue to operate. After replacing the line break character, it can be displayed on one line
[root@localhost ~]# sed '/compl/N;s/\n//' test 
this is a test file , if this first line not display complete , use next

//Note: in this example, we need to know where to split into two lines and where to specify the embedded newline character

There are only two lines in the above demonstration. Now we add another line. After creating the multi line mode, it may appear confusing when the command line is processed again. You can write a script for viewing

[root@localhost ~]# cat test 
 not display compl
ete , use next 
 three line test next 
 
//The following test displays three lines on one line, with a space in front of the third line three
[root@localhost ~]# vi sedscr

/compl/{
N         //Create multiline mode space
s/\n//    //Match newline and cancel empty
N         //Match again to create multiline pattern space
s/\n //   //Match newline and cancel empty
}
[root@localhost ~]# sed -f sedscr test 
this is a test file , if this first line not display complete , use next three line test next  
//Although the above example looks like it is demonstrated on two lines, it is actually on one line, only one line is displayed here

//If you feel it is troublesome to write in the text, you can also choose to enter it on the command line, and the effect is the same
[root@localhost ~]# sed '/compl/{N;s/\n//;N;s/\n //}' test 
this is a test file , if this first line not display complete , use next three line test next  

Multiline deletion

The delete command (d) deletes the contents of the mode space and causes a new input line to be read

The delete command (D) deletes this part of the pattern space until the first embedded newline character, which does not cause a new line to be read in

Difference between D and D

  • d delete the matched pattern space content for the pattern space
  • D for the pattern space, the first row of the pattern space is deleted

The following examples demonstrate the difference between D and D:

//Use D
[root@localhost ~]# cat test 
this is a test file , if this first line not display compl
ete , use next 
 three line test next  
[root@localhost ~]# sed '/compl/{N;D}' test 
ete , use next 
 three line test next   
 
//Use d
[root@localhost ~]# cat test 
this is a test file , if this first line not display compl
ete , use next 
 three line test next  
[root@localhost ~]# sed '/compl/{N;d}' test 
 three line test next  

The following example demonstrates multiple use of D:

//Use D once
[root@localhost ~]# sed '/compl/{N;D}' test 
ete , use next 
 three line test next 
 
//Use twice
[root@localhost ~]# sed '/compl/{N;D};/use/{N;D}' test 
 three line test next 

Tags: Linux shell

Posted on Fri, 17 Sep 2021 05:16:15 -0400 by corillo181