我在Linux中尝试了grep -v '^$',但没有工作。该文件来自Windows文件系统。
当前回答
保持简单。
grep . filename.txt
其他回答
与之前的答案相同:
grep -v -e '^$' foo.txt
这里,grep -e表示grep的扩展版本。'^$'表示^(行首)和$(行尾)之间没有任何字符。'^'和'$'是正则表达式字符。
因此,命令grep -v将打印所有不匹配此模式的行(^和$之间没有字符)。
这样,空行就被消除了。
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
Egrep -v "^\s\s+"
Egrep已经做了正则表达式,\s是空白。
+复制当前模式。
^表示开始
从文件中读取行排除空行
grep -v '^$' folderlist.txt
folderlist.txt
folder1/test
folder2
folder3
folder4/backup
folder5/backup
结果如下:
folder1/test
folder2
folder3
folder4/backup
folder5/backup
试试下面的方法:
grep -v -e '^$' foo.txt
e选项允许正则表达式模式进行匹配。
^$周围的单引号使它适用于shell。其他shell只接受单引号或双引号。
更新:这适用于一个有空行或“全空白”的文件(如windows行带\r\n样式的行结束符),而上面只删除有空行和unix样式行结束符的文件:
grep -v -e '^[[:space:]]*$' foo.txt