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


当前回答

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

其他回答

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

文件中的行中有空白字符吗?

如果是,那么

grep “\S” 文件.txt

否则

握。文件.txt

得到的答案: https://serverfault.com/a/688789

从文件中读取行排除空行

grep -v '^$' folderlist.txt

folderlist.txt

folder1/test

folder2
folder3

folder4/backup
folder5/backup

结果如下:

folder1/test
folder2
folder3
folder4/backup
folder5/backup

如果在一行中有多个空行序列,并且每个序列只希望有一个空行,请尝试

grep -v "unwantedThing" foo.txt | cat -s

Cat -s抑制重复的空输出行。

你的输出会从

match1



match2

to

match1

match2

原始输出中的三个空行将被压缩或“压缩”为一个空行。

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

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