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

Jul 19, 2022 pm 06:53 PM
linux

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

See all articles