Sed stands for Stream Editor and is used for basic text processing in Linux. The sed command is one of the most important commands in Linux and plays an important role in file processing. Can be used to delete or move a particular row that matches a given pattern.

It can also delete specific lines in a file, it can remove expressions from a file, and files can be identified by specifying delimiters such as commas, tabs, or Spaces.

This article lists 15 examples to help you master the sed command.

If you can understand and remember these commands, they can save you a lot of time when you need to use sed.

Note: For the sake of the demonstration, I am not using the -i option when I run sed (because this will directly modify the contents of the file), and the contents of the file whose lines have been removed will be printed to the Linux terminal.

However, if you want to remove lines from the source file in a real environment, use the -i option in the sed command.

Before the demo, I created the sed-demo.txt file and added the following and line numbers to make it easier to understand.

# 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
Copy the code

1) How to delete the first line of a file?

Use the following syntax to delete the first line of the file.

N represents the NTH line in the file, and the d option is used in the sed command to delete a line.

Grammar:

sed 'Nd' file
Copy the code

Use the following sed command to delete the first line in sed-demo.txt.

# sed '1d' sed-demo.txt

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

2) How to delete the last line of a file?

Use the following syntax to delete the last line of the file.

The $sign 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

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

3) How to delete a specified row?

Use the following sed command to delete line 3 in sed-demo.txt.

# sed '3d' sed-demo.txt

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

4) How to delete rows in a specified range?

Use the following sed command to delete lines 5 through 7 in sed-demo.txt.

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

1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
8 Debian
9 Ubuntu
10 openSUSE
Copy the code

5) How to delete multiple lines of content?

The sed command deletes a given set of lines.

In this example, the following sed command removes lines 1, 5, 9, and the last line.

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

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

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 '3, 6! d' sed-demo.txt

3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
Copy the code

6) How to delete blank lines?

Use the following sed command to delete the blank lines in sed-demo.txt.

# sed '/^$/d' 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
Copy the code

7) How do I delete rows that contain a schema?

Use the following sed command to delete lines that match System mode in sed-demo. TXT.

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

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

8) How do I delete a line that contains a string in the string collection?

Use the following sed command to delete lines in seed-demo. TXT that match System or Linux expressions.

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

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

9) How do I delete lines that start with a specified character?

To test, I created the sed-demo-1.txt file and added the following.

# cat sed-demo-1.txt

Linux Operating System
Unix Operating System
RHEL
Red Hat
Fedora
debian
ubuntu
Arch Linux - 1
2 - Manjaro
3 4 5 6
Copy the code

Use the following sed command to delete all lines that begin with the R character.

# sed '/^R/d' sed-demo-1.txt

Linux Operating System
Unix Operating System
Fedora
debian
ubuntu
Arch Linux - 1
2 - Manjaro
3 4 5 6
Copy the code

Use the following sed command to delete all lines beginning with the R or F character.

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

Linux Operating System
Unix Operating System
debian
ubuntu
Arch Linux - 1
2 - Manjaro
3 4 5 6
Copy the code

10) How to delete lines ending in a specified character?

Use the following sed command to delete all lines ending in the m character.

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

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

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

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

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

11) How do I delete all lines beginning with a capital letter?

Use the following sed command to delete all lines beginning with an uppercase letter.

# sed '/^[A-Z]/d' sed-demo-1.txt

debian
ubuntu
2 - Manjaro
3 4 5 6
Copy the code

12) How to delete rows that match a pattern in a specified range?

Use the following sed command to remove lines 1 through 6 that contain Linux expressions.

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

2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE
Copy the code

13) How do I delete a row that matches a pattern and its next line?

Use the following sed command to delete the line containing the System expression and the line following it.

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

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

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.txt

Linux Operating System
Unix Operating System
RHEL
Red Hat
Fedora
debian
ubuntu
Copy the code

Use the following sed command to delete all lines that begin with a number.

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

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

Use the following sed command to delete all lines that end in numbers.

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

Linux Operating System
Unix Operating System
RHEL
Red Hat
Fedora
debian
ubuntu
2 - Manjaro
Copy the code

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, 4, 5 6Copy the code

Via: www.2daygeek.com/linux-remov…

By Magesh Maruthamuthu: Lujun9972

This article was originally compiled by LCTT and released with honor by Linux China