从文件中删除空行 [英] Delete empty lines from a file
本文介绍了从文件中删除空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要删除文件中的空行(仅包含空格-不包含空记录).
I need to delete empty lines from a file (with spaces only - not null records).
以下命令仅适用于空行,不适用于以下情况:空格:
The following command works only for null rows, but not in case of spaces:
sed'/^ $/d'文件名
可以使用grep完成吗?
Can it be done using grep?
推荐答案
对于仅包含空格的空白行,请使用 \ s *
:
Use \s*
for blank lines containing only whitespace:
sed '/^\s*$/d' file
要将更改保存回文件,请使用 -i
选项:
To save the changes back to the file use the -i
option:
sed -i '/^\s*$/d' file
正则表达式 ^ \ s * $
匹配仅包含空格的行, grep -v
打印行与给定模式不匹配,因此将打印以下内容所有都不是黑线:
The regex ^\s*$
matches a line that only contains whitespace, grep -v
print lines that don't match a given pattern so the following will print all none black lines:
grep -v '^\s*$' file
这篇关于从文件中删除空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文