Sed advanced command

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 

Multiline printing

The multiline print P outputs the first part of the multiline mode space until the embedded newline character, that is, the content of the first line

  • Here is an example:
[root@localhost ~]# cat test 
Here are example of the UNIX
System. Where UNIX
System appears, it should be the UNIX
Operation System.

[root@localhost ~]# vi sedscr

/UNIX$/{
N
/\nSystem/{
s// Operation &/
P
D
}
}

[root@localhost ~]# sed -f sedscr test 
Here are example of the UNIX Operation 
System. Where UNIX Operation 
System appears, it should be the UNIX
Operation System.

hold space

The mode space is a buffer that holds the current input line. The contents of the mode space can be copied to the holding space, and the contents of the holding space can also be copied to the mode space. A group of commands can be used to move data between the holding space and the mode space, and the holding space is used for temporary storage. Separate commands cannot act on the holding space or change its contents

Commands that affect schema space

commandabbreviationfunction
HoldH or HCopy or append the contents of the schema space to the hold space
GetG or GCopy or append the contents of the hold space to the pattern space
ExchangexExchange the contents of hold space and pattern space

h: Overwrite schema space content to hold space
H: Append schema space content to hold space
g: Overwrite hold space content into pattern space
G: Append hold space content to mode space

Note: the above commands are based on the perspective of mode space

  • The following example demonstrates the use of keeping space:
[root@localhost ~]# cat test 
1
2
11
22
111
222

[root@localhost ~]# vi sedscr 

//Write a text so that all lines with 1 and lines with 2 are interchanged
/1/{   //Rows matching 1
h      //Put 1 row into hold space
d      //Delete schema space content
}
/2/{    //The row matching 2 is now the row of 2 in the pattern space   
G       //Append the row of holding space 1 to the mode space, and then print it out
}

[root@localhost ~]# sed -f sedscr test 
2
1
22
11
222
111

  • The following example demonstrates replacing a phrase in a line with uppercase
//Prepare a text
[root@localhost ~]# cat test 
find the Match statement dhadaldklsa
dsadak the lisi statement dadhadalk
dadada the zhangsan statement djkajdasljd

//Prepare to replace Match in the first line, lisi in the second line and zhangsan in the third line with uppercase
[root@localhost ~]# cat sedscr 
/the .* statement/{               //Matching content
h    //Add matching rows to hold space
s/.*the (.*) statement.*/\1/     //This is equivalent to leaving only the content that needs to be replaced with uppercase
y/abcdefghijklmnopqrstuvwxyz/BCDEFGHIJKLMNOPQRSTUVWXYZ/           //Replace with uppercase
G          //Append the contents of the holding space to the mode space. At this time, the mode space is a multi line mode space. The first line is the more capitalized content, and the next line is the content of the first line
s/(.*)\n(.*the) .*( statement.*)/\2\1\3/            //Replace the two lines of the mode space with one line
}

Tags: Linux shell vim

Posted on Sat, 18 Sep 2021 16:17:46 -0400 by kiju111