Home > Operation and Maintenance > Linux Operation and Maintenance > How to delete certain lines of a file in Linux

How to delete certain lines of a file in Linux

青灯夜游
Release: 2022-07-19 18:53:31
Original
19644 people have browsed it

In Linux, you can use the sed command to delete certain lines of the specified file. Execute the "sed 'M,Nd' file name" statement to delete the contents of consecutive lines from M to N. Execute "sed ' N1d;N2d;N3d;$d' file name" statement can delete the contents of discontinuous lines N1, N2, and N3. Execute the "sed '/specific character/d' file name" statement to delete lines containing specific characters. Execute The "sed '/^Specific character/d' file name" statement can delete lines starting with specific characters.

How to delete certain lines of a file in Linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

In Linux, you can use the sed command to delete certain lines of a specified file.

sed is the abbreviation of Stream Editor. It is used for basic text conversion in Linux. It is an important command for file operations. Therefore, we can also use it to achieve text deletion operations.

The following are some usage examples of sed commands, covering most usage scenarios. They help you learn the sed command from shallow to deep, allowing you to easily and efficiently delete specific lines of files.

First we prepare a demonstration file sed-demo.txt and take a look at the file content

cat sed-demo.txt
Copy after login

How to delete certain lines of a file in Linux

Then we can use the sed command to experiment.

1. Delete a certain line

First, we start by deleting a certain line, such as deleting the first line and the last line. In fact, It's the Nth row.

The command format to delete the Nth line:

sed 'Nd' file
Copy after login

Let’s try to delete the first line:

sed '1d' sed-demo.txt
Copy after login

How to delete certain lines of a file in Linux

Very Simple, right? There won’t be much explanation here. If you want to delete the content of which line, you only need to replace 1 in the command and it will be ok.

Then the question comes, what number is used to represent the last line? Here is a little tip for you, you can use the dollar sign $ to represent the end, so the command to delete the last line can be written like this:

sed '$d' sed-demo.txt
Copy after login

How to delete certain lines of a file in Linux

2. Delete Certain lines

#sed command can delete consecutive or non-consecutive lines.

Delete consecutive rows, for example, delete the content from rows 5 to 7:

sed '5,7d' sed-demo.txt
Copy after login

How to delete certain lines of a file in Linux

##Delete non-consecutive rows, for example, delete the 1st and 5th rows , the 9th and the last line:

sed '1d;5d;9d;$d' sed-demo.txt
Copy after login

How to delete certain lines of a file in Linux

In addition, it can also be used with logical NOT!, such as deleting other lines except lines 3 to 6:

sed '3,6!d' sed-demo.txt
Copy after login

How to delete certain lines of a file in Linux

3. Delete blank lines

sed also supports deleting blank lines in files, the command is as follows:

sed '/^$/d' sed-demo.txt
Copy after login

How to delete certain lines of a file in Linux

Tip: The expressions between the two slashes / / here play the role of text matching. You can refer to the usage of regular expressions. Below are some commonly used methods to deepen everyone's learning.

4. Delete lines containing specific characters

Suppose we want to delete lines containing the word System in the sample file, we You can use /System/, which means that the string System will be matched if it appears. The specific command is as follows:

sed '/System/d' sed-demo.txt
Copy after login

How to delete certain lines of a file in Linux

Not only that, we can also add logical conditions , for example, the following command:

sed '/System\|Linux/d' sed-demo.txt
Copy after login

How to delete certain lines of a file in Linux

The symbol

\| represents logical OR. The above command means that all lines with System or Linux in the text are To delete.

5. Delete lines starting with specific characters

First, we create another sample file sed-demo-1.txt for better Demonstration, the content is as follows:

cat sed-demo-1.txt
Copy after login

How to delete certain lines of a file in Linux

As mentioned above, the $ sign can be understood as the end, so are there any characters that can represent the beginning? The answer is yes, here we can use the ^ sign to represent the beginning.

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

sed '/^R/d' sed-demo-1.txt
Copy after login

1How to delete certain lines of a file in Linux

那么问题来了,比如我想删除以 R 或者 F 开头的行,那我是不是要执行两次命令呢?如果是有更多岂不是要执行多次命令?这里它有一个简单的写法,你只要把这些字符写在一对中括号 [] 里就可以了:

sed '/^[RF]/d' sed-demo-1.txt
Copy after login

1How to delete certain lines of a file in Linux

上面命令的作用是 删除以 R 或者 F 开头的行。

6. 删除特定字符结尾的行

同上面一个道理,删除以某一个字符结尾的行,比方说删除以 m 结尾的行,我们可以这样做:

sed '/m$/d' sed-demo.txt
Copy after login

1How to delete certain lines of a file in Linux

删除以 x 或 m 结尾的行可以这样写:

sed '/[xm]$/d' sed-demo.txt
Copy after login

1How to delete certain lines of a file in Linux

7. 删除以大写字母开头的行

这里问题又来了,我想要删除所有以大写字母开头的行呢?按照上面的做法是不是要将 A 到 Z 这 26 个字母都写进 [ ] 里呢?其实我们大可不必这样做,在 A 和 Z 中间加个 - 就可以了:

sed '/^[A-Z]/d' sed-demo-1.txt
Copy after login

1How to delete certain lines of a file in Linux

机智的你看到这里肯定会想到其他类似的用法的了,不妨看看下面是否有你想到的命令吧。

8. 删除包含字母字符的行

sed '/[A-Za-z]/d' sed-demo-1.txt
Copy after login

1How to delete certain lines of a file in Linux

9. 删除包含数字的行

sed '/[0-9]/d' sed-demo-1.txt
Copy after login

1How to delete certain lines of a file in Linux

另外,通过这个例子,我们可以加上 ^ 和 $ 更好地看到他们之间的区别:

sed '/^[0-9]/d' sed-demo-1.txt
Copy after login

1How to delete certain lines of a file in Linux

sed '/[0-9]$/d' sed-demo-1.txt
Copy after login

1How to delete certain lines of a file in Linux

相关推荐:《Linux视频教程

The above is the detailed content of How to delete certain lines of a file in Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template