我在Linux中尝试了grep -v '^$',但没有工作。该文件来自Windows文件系统。


当前回答

我努力尝试,但这似乎工作(假设\r在这里咬你):

printf "\r" | egrep -xv "[[:space:]]*"

其他回答

使用Perl:

perl -ne 'print if /\S/'

\S表示匹配非空字符。

保持简单。

grep . filename.txt

此代码删除空行和以“#”开头的行

 grep -v "^#" file.txt | grep -v ^[[:space:]]*$
grep -v "^[[:space:]]*$"

The -v makes it print lines that do not completely match

===Each part explained===
^             match start of line
[[:space:]]   match whitespace- spaces, tabs, carriage returns, etc.
*             previous match (whitespace) may exist from 0 to infinite times
$             match end of line

〇运行代码

$ echo "
> hello
>       
> ok" |
> grep -v "^[[:space:]]*$"
hello
ok

要了解更多关于这是如何/为什么工作的,我建议阅读正则表达式。http://www.regular-expressions.info/tutorial.html

awk 'NF' file-with-blank-lines > file-with-no-blank-lines