sed advanced command N, D

Keywords: Linux regex perl

sed advanced command

brief introduction

Sed is a stream editor. It is a very important tool in text processing. It can be used perfectly with regular expressions and has extraordinary functions. During processing, the currently processed line is stored in a temporary buffer called "pattern space", and then the contents in the buffer are processed with sed command. After processing, the contents of the buffer are sent to the screen. The next line is then processed and repeated until the end of the file. The contents of the file will not change unless you use redirection to store the output. Sed is mainly used to automatically edit one or more files; Simplify the repeated operation of documents; Write conversion program, etc.

sed is a powerful non interactive streaming text editor under Linux. It can add, delete, modify and query text files. It supports matching text content by line, field and rule. It is flexible and convenient, especially suitable for editing large files. This article mainly introduces some basic uses of sed, and demonstrates the use of sed through shell script.

There are two syntax for calling sed command:

I. specify sed instruction on the command line to process text: sed + option 'instruction' file

2. First save the sed instruction to a file and call the file as a parameter: the sed + option - f contains the file of the sed instruction

Common options for sed:

- r: use extended regular expressions

- E: it tells sed to interpret the next parameter as a sed instruction. The - e option is required only when multiple sed instructions are given on the command line

- f: the file followed by the sed instruction

- i: modify the content directly. If - i is not added, it is only preview by default, and the file will not be actually modified

- N: cancel the default output. sed will output all text contents by default. After using the - n parameter, only the processed lines will be displayed

Edit commands in sed:

a: append and insert content after the matching line

c: change the content of the matching line

i: insert content before matching line

d: delete the matching content

s: replace the matching content

p: print out the matching content, usually with the - n option and

=: used to print the line number of the matched line

N: read the next line. When n is encountered, it will automatically jump to the next line

sed execution process

[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG gkdevumt-1631839513704) (C: \ users \ 14375 \ desktop \ professional \ Linux\QQ image 20210916162721.png)]

sed advanced command N, D

commandfunction
Next (N)Add the next row in the data flow to create a multi row group for processing
Delete(D)Delete a row in a multiline group

N multiline operation command

The N command will add the text content of the next line after the existing data in the buffer (separated by a newline character), so that the first and second text lines are located in the buffer at the same time. The sed command will treat the two lines of data as one line.

#The SED command looks for the line of text that contains the word first. When the line is found, it merges the next line into that line with the N command, and then replaces the newline character with a space with the replace command s. As a result, two lines in the text file become one line in the output of sed
[root@localhost ~]# cat luo.txt 
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.

[root@localhost ~]# sed '/first/{N;s/\n/ /}' luo.txt 
This is the header line.
This is the first data line. This is the second data line.
This is the last line.

D multiline delete command

sed provides not only a single line delete command (d), but also a multi line delete command D. its function is to delete only the first line in the buffer, that is, the D command deletes the contents before the first line break (including line break) in the buffer. If the pattern space contains a newline character, the text in the pattern space up to the first newline character is deleted, the new input line is not read, and the loop is restarted using the synthesized pattern space. If the pattern space does not contain a newline character, a normal new loop is started like issuing the D command.

#	sed looks for a blank line and adds the next line to the buffer with the N command. At this point, if the contents of the buffer contain the word header, the D command deletes the first line in the buffer.
[root@localhost ~]# cat lcr.txt 

On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrators should attend.

Thank you for your attendance.

[root@localhost ~]# sed '/^$/{N ; /System/D}' lcr.txt 
On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrators should attend.

Thank you for your attendance.

##	When two empty lines are encountered, the D command deletes only the first of the two empty lines. The next time the script is read, the empty line will be read into the schema space by another line. If that line is not empty, both lines are output, so it is ensured that an empty line is output. The white point is that when there are two empty lines in the pattern space, only the first empty line is deleted. When an empty line is followed by text, the content in the pattern space is not processed.
[root@localhost ~]# cat lcr.txt 
On Tuesday, the Linux System


Administrator's group meeting will be held.



All System Administrators should attend.

Thank you for your attendance.

[root@localhost ~]# sed '/^$/{N;/^\n$/D}' lcr.txt
On Tuesday, the Linux System

Administrator's group meeting will be held.

All System Administrators should attend.

Thank you for your attendance.

Posted by lmninfo on Fri, 17 Sep 2021 02:50:47 -0700