How to efficiently delete specific lines of files by using sed command

  • 2021-08-28 21:47:23
  • OfStack

Preface

Normally, we want to delete some lines in a file. Generally, we open the file first, then find the contents to be deleted, then select these lines and press the delete key to delete them, which is no problem when the amount of data is small. However, 1 denier file in the line of data is very much, and the data jumbled, you have to use the above method to do it is very horrible. For this reason, today's article will take you to learn how to use sed command line tools to efficiently and elegantly delete specific lines in files even when there are many and miscellaneous data.

sed is the abbreviation of Stream Editor. It is used for basic text conversion in Linux and is an important command for file operation. Therefore, we can also use it to realize text deletion operation.

The following are some examples of using sed commands, covering most usage scenarios, helping you learn sed commands from simple to deep, and making it easy for you to efficiently delete specific lines of files.

First, we prepare a demonstration file sed-demo. txt.


# cat sed-demo.txt

1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

Then we can use sed command to experiment.

Note:-i means that the file operation is performed directly, and the results are not displayed on the terminal. Because this is a demonstration, we don't have the-i option here. In practice, please take the-i option.

1. Delete a 1 line

First of all, let's start by deleting a certain line, such as deleting the first line and the last line, which is actually the line N.

Delete the command format of line N:


sed 'Nd' file

Let's try deleting line 1:


# sed '1d' sed-demo.txt

After deletion:
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

It's simple, isn't it? I won't explain much here. You only need to replace 1 in the command for ok if you want to delete which line.

Then the question comes. What number does the last line use? Here is a little tip for you. You can use the dollar sign $to indicate the last line, so the command to delete the last line can be written as follows:


# sed '$d' sed-demo.txt

After deletion:
1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu

2. Delete some rows

The sed command can delete contiguous or discontiguous lines.

Delete consecutive lines, for example, delete the contents from 5 to 7 lines:


# sed '5,7d' sed-demo.txt

After deletion:
1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
8 Debian
9 Ubuntu
10 openSUSE

Delete discontinuous rows, such as the first, fifth, ninth, and last row:


# sed '1d;5d;9d;$d' sed-demo.txt

After deletion:

2 Unix Operating System
3 RHEL
4 Red Hat
6 Arch Linux
7 CentOS
8 Debian

In addition, it can also cooperate with logic! Use, such as deleting lines other than lines 3 to 6:


# sed '3,6!d' sed-demo.txt

After deletion:

3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux

3. Delete blank lines

sed also supports deleting blank lines of files with the following command:


# sed '/^$/d' sed-demo.txt

After deletion:

1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

Tip: The expressions in the two slashes//here play the role of text matching. You can refer to the use of regular expressions. Here are some common methods to deepen your study.

4. Delete lines that contain specific characters

Suppose we want to delete the line in the sample file that contains the word System, we can use/System/, which means that the string System is matched. The specific command is as follows:


# sed '/System/d' sed-demo.txt

After deletion:

3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

Not only that, but we can also add 1 logical condition, such as the following command:


# sed '/System\|Linux/d' sed-demo.txt

After deletion:

3 RHEL
4 Red Hat
5 Fedora
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

The symbol\ stands for logical OR, and the above command means that lines with System or Linux in the text should be deleted.

5. Delete lines that begin with specific characters

First, we create another sample file, sed-demo-1. txt, for a better demonstration, which reads as follows:


sed 'Nd' file
0

As mentioned above, the $sign can be understood as the end, so is there any character that can represent the beginning? The answer is yes. Here we can start with the symbol ^.

Then, when we want to delete a line that begins with a certain 1 character, such as a line that begins with R, we can use the following command:


sed 'Nd' file
1

Then the question arises. For example, if I want to delete a line that begins with R or F, do I have to execute the command twice? If there are more, wouldn't it be necessary to execute multiple orders? Here it has a simple way to write it. You just have to write these characters in a pair of brackets []:


sed 'Nd' file
2

The purpose of the above command is to delete lines that begin with R or F.

6. Delete lines ending in specific characters

In the same way as above, to delete a line ending in a certain 1 character, for example, to delete a line ending in m, we can do this:


# sed '/m$/d' sed-demo.txt

After deletion:
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

Deleting a line ending in x or m can be written as follows:


sed 'Nd' file
4

7. Delete lines that begin with uppercase letters

Here's the problem again. Do I want to delete all lines that start with capital letters? Do you want to write the 26 letters A to Z into [] according to the above practice? In fact, we don't have to do this, just add a-between A and Z:


sed 'Nd' file
5

Witty you will definitely think of other similar usages when you see here. Let's see if there are any commands you think of below.

8. Delete lines that contain alphabetic characters


sed 'Nd' file
6

9. Delete rows that contain numbers


sed 'Nd' file
7

In addition, through this example, we can better see the difference between them by adding ^ and $:


# sed '/^[0-9]/d' sed-demo-1.txt

After deletion:

Linux Operating System
Unix Operating System
RHEL
Red Hat
Fedora
debian
ubuntu
Arch Linux - 1


sed 'Nd' file
9

10. More

In fact, the content of the file we want to delete is more specific, and simple conditions can't meet our needs, so sed also supports more complex combination of conditions. For example, if I want to specify to delete the content with the word Linux in lines 1 to 6, then:


# sed '1,6{/Linux/d;}' sed-demo.txt

After deletion:
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

Delete the content containing System and its next 1 line:


# sed '/System/{N;d;}' sed-demo.txt

After deletion:
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

Summarize


Related articles: