linux命令行输出文本
On Linux, how to delete a specific line from a text file in command line? For example, to delete the 4th line from a file
在Linux上 ,如何在命令行中从文本文件中删除特定行? 例如,从文件中删除第四行
aaa
bbb
ccc
ddd
eee
ffffff
You can use the “stream editor for filtering and transforming text” sed.
您可以使用“流编辑器来过滤和转换文本” sed 。
With GNU sed:
使用GNU sed:
sed -i '4d' ./file
Here, -i means edit the file inplace. d is the command to “delete the pattern space; immediately start next cycle”. 4 means the 4th line.
在这里, -i表示就地编辑文件。 d是“删除模式空间 ; 立即开始下一个周期”。 4表示第四行。
The file content will be:
文件内容为:
aaa
bbb
ccc
eee
ffffff
There are more combinations for deleting lines. Some examples are:
还有更多删除行的组合。 一些例子是:
Remove the last line:
删除最后一行:
sed '$d' filename.txt
Remove all empty lines:
删除所有空行:
sed '/^$/d' ./file
or
要么
sed '/./!d' ./file
Remove lines from 7 to 9:
从7到9删除行:
sed '7,9d' ./file
Remove the line matching by a regular expression REGULAR:
用正则表达式REGULAR删除匹配的行:
sed '/REGULAR/d' ./file
For a simple example, remove the lines containing “oops”:
举一个简单的例子,删除包含“ oops”的行:
sed '/oops/d' ./file
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/how-to-delete-a-specific-line-from-a-text-file-in-command-line-on-linux/
linux命令行输出文本