In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how linux uses the sed command to efficiently delete specific lines of a file. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
Efficiently delete specific lines of a file using the sed command
Normally, when we want to delete some lines in a file, we usually open the file first, then find out what we want to delete, then select these lines and press the delete key to delete them, which is no problem when the amount of data is small. However, once there is a lot of line data in the file, and the data is jumbled, it will be scary if you have to use the above method to do it. To that end, today's article will take you to learn how to use the sed command-line tool to efficiently and elegantly delete specific lines from a file, even when there is a lot of data.
Sed is the abbreviation of Stream Editor, it is used for basic text conversion in Linux, and it is an important command for file operation, so we can also use it to delete text.
Here are some examples of the use of sed commands, covering most of the usage scenarios, helping you learn sed commands from simple to deep, so that you can easily delete specific lines of files efficiently.
First of all, let's 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 the sed command to experiment.
Note:-I means to operate the file directly without displaying the result on the terminal. Because it's a demonstration, we don't have the-I option here. In practice, please take the-I option.
1. Delete a row
First of all, let's start by deleting a certain line, such as the first line and the last line, which is actually line N.
Delete the command format for line N:
Sed 'Nd' file
Let's try deleting the first line:
# 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 easy, isn't it? There is no more explanation here, you only need to replace the 1 in the command to ok what line you want to delete.
So the question is, what is the number on the last line? Here's a tip. You can use the dollar sign $to indicate the end, so the command to delete the last line can be written like this:
# 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
two。 Delete some lines
The sed command can delete contiguous or discontiguous line content.
Delete consecutive lines, for example, from 5 to 7 lines:
# sed '5openSUSE 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 discontiguous lines, such as lines 1, 5, 9, and last:
# sed'1d CentOS 5d 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 the logic is not! Use, such as deleting lines other than lines 3 to 6:
# sed'3 Fedora 6 Fedora 'sed-demo.txt After deletion: 3 RHEL 4 Red Hat 5 Fedora 6 Arch Linux
3. Delete a blank line
Sed also supports deleting blank lines of a file, 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 / / play the role of text matching, you can refer to the use of regular expressions. Here are some common ways to deepen your study.
4. Delete lines that contain specific characters
Suppose we want to delete the line containing the word System in the sample file, we can use / System/, to indicate that the string System is matched as soon as it appears. 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, we can also add some logical conditions, 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. The above command means that all lines with System or Linux in the text should be deleted.
5. Delete lines at the beginning of a specific character
First, let's create another sample file, sed-demo-1.txt, for a better demonstration, which is 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 to represent the beginning? The answer is yes. Here we can start with a ^ sign.
So, when we want to delete a line that begins with a character, such as a line that begins with R, we can use the following command:
# sed'/ ^ R sed-demo-1.txt After deletion: Linux Operating System Unix Operating System Fedora debian ubuntu Arch Linux-1 2-Manjaro 3 4 5 6
So the question is, if I want to delete a line that starts with R or F, do I have to execute the command twice? If there are more, don't you have to execute multiple orders? Here it has a simple way to write it, you just have to write these characters in a pair of square 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 deletes lines that begin with R or F.
6. Delete lines at the end of a specific character
For the same reason, to delete a line that ends with a character, for example, to delete a line that ends with m, we can do this:
# sed'/ Ubuntu sed-demo.txt After deletion: 3 RHEL 4 Red Hat 5 Fedora 6 Arch Linux 7 CentOS 8 Debian 9 Ubuntu 10 openSUSE
To delete a line that ends in x or m, you can write:
# 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 that begin with uppercase letters
Here comes the question again. Do I want to delete all lines that start with capital letters? According to the above practice, do you want to write all the 26 letters A to Z in []? In fact, we don't have to do this, just add a-between An and Z:
# sed'/ ^ [Amurz] / d'sed-demo-1.txt After deletion: debian ubuntu 2-Manjaro 3 4 5 6
Witty, you will certainly think of other similar uses when you see this. You might as well see if there are any commands you can think of below.
8. Delete lines that contain alphabetic characters
# sed'/ [A-Za-z] / d 'sed-demo-1.txt After deletion: 3 4 5 6
9. Delete rows that contain 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 the three:
# sed'/ ^ [0-9] / d'sed-demo-1.txt After deletion: Linux Operating System Unix Operating System RHEL Red Hat Fedora debian ubuntu Arch Linux-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 content of the file we want to delete is more specific, and simple conditions cannot meet our needs, so sed also supports more complex combinations of conditions. For example, if I want to specify to delete the word Linux in lines 1 to 6, then:
# sed'1 sed-demo.txt After deletion 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/ {Nbatd;} 'sed-demo.txt After deletion: 3 RHEL 4 Red Hat 5 Fedora 6 Arch Linux 7 CentOS 8 Debian 9 Ubuntu 10 openSUSE and above are all the contents of the article "how to use the sed command to efficiently delete specific lines of a file". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.