linux使用sed删除文本文件中的所有空行

Linux如何删除文件中所有空行?

示例文件

[root@golinuxhub ~]# cat /tmp/file
This is line one

This is line two
This is line three

This is line four

[root@golinuxhub ~]#

使用下面的命令从文本文件中删除所有空白行

[root@golinuxhub ~]# sed -e '/^$/d' /tmp/file
This is line one
This is line two
This is line three
This is line four
[root@golinuxhub ~]#

其中使用的正则表达式是
^-表示以...开始
$-表示以...结束

由于我们没有在我们的正则表达式之间提供任何其他字符,所以这是一个将它看作空白行的符号。

在同一个文件使用 -i参数执行操作(直接修改文件,而不是打印结果):

[root@onitroad ~]# sed -i '/^$/d' /tmp/file

[root@onitroad ~]# cat /tmp/file
 This is line one
 This is line two
 This is line three
 This is line four
日期:2020-06-02 22:17:32 来源:oir作者:oir