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


当前回答

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

其他回答

我更喜欢使用egrep,尽管在我的测试中,您的方法工作得很好(尽管在我的测试中没有引号)。这个方法也奏效了:

egrep -v "^(\r?\n)?$" filename.txt

与之前的答案相同:

grep -v -e '^$' foo.txt

这里,grep -e表示grep的扩展版本。'^$'表示^(行首)和$(行尾)之间没有任何字符。'^'和'$'是正则表达式字符。

因此,命令grep -v将打印所有不匹配此模式的行(^和$之间没有字符)。

这样,空行就被消除了。

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

从文件中读取行排除空行

grep -v '^$' folderlist.txt

folderlist.txt

folder1/test

folder2
folder3

folder4/backup
folder5/backup

结果如下:

folder1/test
folder2
folder3
folder4/backup
folder5/backup

Egrep -v "^\s\s+"

Egrep已经做了正则表达式,\s是空白。

+复制当前模式。

^表示开始