Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the sed command correctly

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use sed command correctly". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Sed is an online editor that processes one line of content at a time. When processing, the currently processed line is stored in a temporary buffer, called "pattern space" (pattern space), and then the contents of the buffer are processed with the sed command, and when the processing is completed, the contents of the buffer are sent to the screen. Then process the next line, which is repeated until the end of the file.

How to use the sed command to delete the lines in a file Sed represents the stream editor Stream Editor, which 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.txtRHELRed HatFedoraArch LinuxCentOSDebianUbuntuopenSUSE

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.txtRHELRed HatFedoraCentOSDebianUbuntuopenSUSE

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.txtRHELRed HatFedoraArch LinuxCentOSDebianUbuntuopenSUSE

Use the following sed command to delete all lines ending with the x or m characters.

# sed'/ [xm] $/ d' sed-demo.txtRHELRed HatFedoraCentOSDebianUbuntuopenSUSE

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.txtUnix Operating SystemRHELRed HatFedoraCentOSDebianUbuntuopenSUSE

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.txtRHELRed HatFedoraArch LinuxCentOSDebianUbuntuopenSUSE

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.txtLinux 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.txt3 456 "how to use the sed command correctly" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report