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


当前回答

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

如果是,那么

grep “\S” 文件.txt

否则

握。文件.txt

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

其他回答

下面是删除白色线条和以#符号开始的线条的另一种方法。我认为这对于读取配置文件非常有用。

[root@localhost ~]# cat /etc/sudoers | egrep -v '^(#|$)'
Defaults    requiretty
Defaults   !visiblepw
Defaults    always_set_home
Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR
LS_COLORS"
root    ALL=(ALL)       ALL
%wheel  ALL=(ALL)       ALL
stack ALL=(ALL) NOPASSWD: ALL

使用Perl:

perl -ne 'print if /\S/'

\S表示匹配非空字符。

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

Use:

grep pattern filename.txt | uniq
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