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--
This article mainly shows you the "Linux vital sed command how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Linux important life sed command how to use" this article.
How to use the sed command to delete lines in a file
Sed stands for flow editor Stream Editor and is often used for basic text processing in Linux. Sed command is one of the most important commands in Linux, which plays an important role in file processing. Can be used to delete or move specific rows that match a given pattern.
It can also delete specific lines in the file, it can delete expressions from the file, and the file can be identified by specifying delimiters such as commas, tabs, or spaces.
This article lists 15 usage examples that can help you master sed commands.
If you can understand and remember these commands, they will come in handy when you need to use sed, saving you a lot of time.
Note: for demonstration purposes, instead of using the-I option when I execute the sed command (because this option directly modifies the contents of the file), the contents of the file with the lines removed will be printed to the Linux terminal.
However, if you want to delete lines from the source file in the real world, use the-I option in the sed command.
Before the demonstration, I created the sed-demo.txt file and added the following and the corresponding line number for better understanding.
# cat sed-demo.txt Linux Operating System Unix Operating System RHEL Red Hat Fedora Arch Linux CentOS Debian Ubuntu openSUSE
1) how do I delete the first line of a file?
Delete the first line of the file using the following syntax.
N represents the Nth line in the file, and the d option is used to delete a line in the sed command.
Syntax:
Sed 'Nd' file
Use the following sed command to delete the first line in sed-demo.txt.
# sed '1d' sed-demo.txt Unix Operating System RHEL Red Hat Fedora Arch Linux CentOS Debian Ubuntu openSUSE
2) how do I delete the last line of a file?
Delete the last line of the file using the following syntax.
The $symbol represents the last line of the file.
Use the following sed command to delete the last line in sed-demo.txt.
# sed'$d' sed-demo.txt Linux Operating System Unix Operating System RHEL Red Hat Fedora Arch Linux CentOS Debian Ubuntu
3) how do I delete a specified line?
Use the following sed command to delete line 3 in sed-demo.txt.
# sed '3d' sed-demo.txt Linux Operating System Unix Operating System Red Hat Fedora Arch Linux CentOS Debian Ubuntu openSUSE
4) how do I delete rows within a specified range?
Use the following sed command to delete lines 5 through 7 in sed-demo.txt.
# sed '5je 7d' sed-demo.txt Linux Operating System Unix Operating System RHEL Red Hat Debian Ubuntu openSUSE
5) how to delete multiple lines of content?
The sed command deletes a collection of given rows.
In this example, the following sed command deletes lines 1, 5, 9, and last.
# sed '1dters5ditter9dittersd'sed-demo.txt Unix Operating System RHEL Red Hat Arch Linux CentOS Debian
5A) how do I delete rows outside the specified range?
Use the following sed command to delete all lines outside the range of lines 3 through 6 in sed-demo.txt.
# sed '3rec 6edd' sed-demo.txt RHEL Red Hat Fedora Arch Linux
6) how to delete blank lines?
Use the following sed command to delete blank lines in sed-demo.txt.
# sed'/ ^ $/ d'sed-demo.txt Linux Operating System Unix Operating System RHEL Red Hat Fedora Arch Linux CentOS Debian Ubuntu openSUSE
7) how do I delete a row that contains a pattern?
Use the following sed command to delete lines in sed-demo.txt that match the System pattern.
# sed'/ System/d' sed-demo.txt RHEL Red Hat Fedora Arch Linux CentOS Debian Ubuntu openSUSE
8) how do I delete a line that contains a string in a string collection?
Use the following sed command to delete lines in sed-demo.txt that match System or Linux expressions.
# sed'/ System\ | Linux/d' sed-demo.txt RHEL Red Hat Fedora CentOS Debian Ubuntu openSUSE
9) how do I delete a line that begins with a specified character?
To test, I created the sed-demo-1.txt file and added the following.
# cat sed-demo-1.txtLinux Operating SystemUnix Operating SystemRHELRed HatFedoradebianubuntuArch Linux-12-Manjaro3 4 5 6
Use the following sed command to delete all lines that begin with the R character.
# sed'/ ^ R sed d'sed-demo-1.txtLinux Operating SystemUnix Operating SystemFedoradebianubuntuArch Linux-12-Manjaro3 4 5 6
Use the following sed command to delete all lines that begin with the R or F character.
# sed'/ ^ [RF] / d 'sed-demo-1.txtLinux Operating SystemUnix Operating SystemdebianubuntuArch Linux-12-Manjaro3 4 5 6
10) how do I delete a line that ends with a specified character?
Use the following sed command to delete all lines at the end of the m character.
# sed'/ massively sed-demo.txt RHEL Red Hat Fedora Arch Linux CentOS Debian Ubuntu openSUSE
Use the following sed command to delete all lines ending with the x or m characters.
# sed'/ [xm] $/ d' sed-demo.txt RHEL Red Hat Fedora CentOS Debian Ubuntu openSUSE
11) how do I delete all lines starting with uppercase letters?
Use the following sed command to delete all lines starting with uppercase letters.
# sed'/ ^ [Amurz] / d 'sed-demo-1.txtdebianubuntu2-Manjaro3 4 5 6
12) how do I delete rows that match patterns within a specified range?
Use the following sed command to delete lines 1 through 6 that contain Linux expressions.
# sed '1Jing 6 {/ Linux/d;}' sed-demo.txt Unix Operating System RHEL Red Hat Fedora CentOS Debian Ubuntu openSUSE
13) how do I delete the line that matches the pattern and its next line?
Use the following sed command to delete the line containing the System expression and its next line.
# sed'/ System/ {Nutterd;} 'sed-demo.txt RHEL Red Hat Fedora Arch Linux CentOS Debian Ubuntu openSUSE
14) how do I delete rows that contain numbers?
Use the following sed command to delete all lines that contain numbers.
# sed'/ [0-9] / d'sed-demo-1.txtLinux Operating SystemUnix Operating SystemRHELRed HatFedoradebianubuntu
Use the following sed command to delete all lines that start with a number.
# sed'/ ^ [0-9] / d'sed-demo-1.txtLinux Operating SystemUnix Operating SystemRHELRed HatFedoradebianubuntuArch Linux-1
Use the following sed command to delete all lines that end with numbers.
# sed'/ [0-9] $/ d 'sed-demo-1.txt Linux Operating SystemUnix Operating SystemRHELRed HatFedoradebianubuntu-Manjaro
15) how do I delete lines that contain letters?
Use the following sed command to delete all lines that contain letters.
# sed'/ [A-Za-z] / d'sed-demo-1.txt 3456 and above are all the contents of this article entitled "how to use Linux's vital sed Command". 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.