


How to delete certain lines of a file in 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.
#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
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
Let’s try to delete the first line:
sed '1d' sed-demo.txt
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
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
sed '1d;5d;9d;$d' sed-demo.txt
sed '3,6!d' sed-demo.txt
3. Delete blank lines
sed also supports deleting blank lines in files, the command is as follows:
sed '/^$/d' sed-demo.txt
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
sed '/System\|Linux/d' sed-demo.txt
\| 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
sed '/^R/d' sed-demo-1.txt
那么问题来了,比如我想删除以 R 或者 F 开头的行,那我是不是要执行两次命令呢?如果是有更多岂不是要执行多次命令?这里它有一个简单的写法,你只要把这些字符写在一对中括号 [] 里就可以了:
sed '/^[RF]/d' sed-demo-1.txt
上面命令的作用是 删除以 R 或者 F 开头的行。
6. 删除特定字符结尾的行
同上面一个道理,删除以某一个字符结尾的行,比方说删除以 m 结尾的行,我们可以这样做:
sed '/m$/d' sed-demo.txt
删除以 x 或 m 结尾的行可以这样写:
sed '/[xm]$/d' sed-demo.txt
7. 删除以大写字母开头的行
这里问题又来了,我想要删除所有以大写字母开头的行呢?按照上面的做法是不是要将 A 到 Z 这 26 个字母都写进 [ ] 里呢?其实我们大可不必这样做,在 A 和 Z 中间加个 - 就可以了:
sed '/^[A-Z]/d' sed-demo-1.txt
机智的你看到这里肯定会想到其他类似的用法的了,不妨看看下面是否有你想到的命令吧。
8. 删除包含字母字符的行
sed '/[A-Za-z]/d' sed-demo-1.txt
9. 删除包含数字的行
sed '/[0-9]/d' sed-demo-1.txt
另外,通过这个例子,我们可以加上 ^ 和 $ 更好地看到他们之间的区别:
sed '/^[0-9]/d' sed-demo-1.txt
sed '/[0-9]$/d' sed-demo-1.txt
相关推荐:《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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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)

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.

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.

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.

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.

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.

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