Use the sed command to delete specific lines of the file efficiently

ed is short for Stream Editor, which is used in Linux Basic text conversion is an important part of file operation comma...

ed is short for Stream Editor, which is used in Linux Basic text conversion is an important part of file operation command Therefore, we can also use it to delete text.

The following are some examples of using the sed command, covering most usage scenarios. It helps you learn the sed command from simple to deep, so that you can easily delete specific lines of files efficiently.

First, we prepare a demo 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 experiment with the sed command.

1. Delete a line

First, let's start by deleting a row, such as the first row and the last row, which is actually the nth row.

Delete the command format on line N:

sed 'Nd' file Let's delete the first line and try: # 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? There is no more explanation here. If you want to delete the contents of the first line, you just need to replace 1 in the command.

The question is, what number is the last line? Here's a little hint. You can use the dollar sign $to represent 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 lines

The sed command can delete continuous or discontinuous lines.

Delete consecutive lines, such as 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 lines, such as the first, fifth, ninth and last lines:

# 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 logical non! 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. The commands are 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 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 learning.

4. Delete lines containing specific characters

Suppose we want to delete the line content containing the word System in the sample file, we can use / System /, which means to match the string System. The specific commands are 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, we can also add the following logical conditions, such as the following commands:

# 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 \ | represents logical or. The above command means that all lines with System or Linux in the text should be deleted.

5. Delete the line beginning with a specific character

First, let's create another sample file sed-demo-1.txt for better demonstration. Its contents are as follows:

# cat sed-demo-1.txt After deletion: Linux Operating System Unix Operating System RHEL Red Hat Fedora debian ubuntu Arch Linux - 1 2 - Manjaro 3 4 5 6

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

Then, when we want to delete a line starting with a certain character, for example, delete a line starting with R, we can use the following command:

# sed '/^R/d' sed-demo-1.txt After deletion: Linux Operating System Unix Operating System Fedora debian ubuntu Arch Linux - 1 2 - Manjaro 3 4 5 6

Then the question arises. For example, if I want to delete the line beginning with R or F, do I have to execute the command twice? If there are more, don't you have to execute multiple commands? There is a simple way to write it here. You just need to write these characters in a pair of brackets []:

# sed '/^[RF]/d' sed-demo-1.txt After deletion: Linux Operating System Unix Operating System debian ubuntu Arch Linux - 1 2 - Manjaro 3 4 5 6

The above command is used to delete lines starting with R or F.

6. Delete lines ending with specific characters

For the same reason as the above, delete the line ending with a certain character, for example, delete the line ending with 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

Delete the line ending with x or m, which can be written as follows:

# sed '/[xm]$/d' sed-demo.txt After deletion: 3 RHEL 4 Red Hat 5 Fedora 7 CentOS 8 Debian 9 Ubuntu 10 openSUSE

7. Delete lines beginning with capital letters

Here's the problem again. Do I want to delete all lines starting with uppercase letters? Do you want to write the 26 letters A to Z in [] according to the above method? In fact, we don't have to do this. Just add A - between A and Z:

# sed '/^[A-Z]/d' sed-demo-1.txt After deletion: debian ubuntu 2 - Manjaro 3 4 5 6 Witty, you will certainly think of other similar usages here. You might as well see if there are any commands you think of below.

8. Delete lines containing alphabetic characters

# sed '/[A-Za-z]/d' sed-demo-1.txt After deletion: 3 4 5 6

9. Delete rows containing numbers

# sed '/[0-9]/d' sed-demo-1.txt After deletion: Linux Operating System Unix Operating System RHEL Red Hat Fedora debian ubuntu

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

# 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 '/[0-9]$/d' sed-demo-1.txt After deletion: Linux Operating System Unix Operating System RHEL Red Hat Fedora debian ubuntu 2 - Manjaro

10. Other more

In fact, the file content we want to delete is more specific, and simple conditions can not meet our needs. Therefore, sed also supports more complex condition combinations. For example, if I want to specify the deletion of 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 that contains System and its next line:

# sed '/System/' sed-demo.txt After deletion: 3 RHEL 4 Red Hat 5 Fedora 6 Arch Linux 7 CentOS 8 Debian 9 Ubuntu 10 openSUSE

3 December 2021, 02:56 | Views: 6266

Add new comment

For adding a comment, please log in
or create account

0 comments