grep 行不以 # 或空行开头 [英] grep lines NOT starting with # or empty lines
问题描述
当我使用 grep
(GNU grep) 2.22 打印时,not 以 #
开头或not强>空我需要做的
When I use grep
(GNU grep) 2.22 to print that do not start by #
or that are not empty I need to do
$ grep -v '^#' fileNameIGrepFor |grep -v '^$'
我觉得这很丑,有没有更聪明的方法(请使用grep
)
I think this is ugly, is there a smarter way (using grep
please)
推荐答案
grep -v '^#' fileNameIGrepFor | grep -v '^$'
可以简化为:
grep -v '^#\|^$' fileNameIGrepFor
要删除丑陋的\
,您可以使用grep -E
,或等效地egrep
:
To remove the ugly \
you can use grep -E
, or equivalently egrep
:
egrep -v '^#|^$' fileNameIGrepFor
然后您可以通过对术语进行分组来澄清这一点:
You could then clarify this a bit by grouping the terms:
egrep -v '^(#|$)' fileNameIGrepFor
然后通过在 #
之前检查空格使其更加健壮:
And then make it a little more robust by including a check for whitespace before the #
:
egrep -v '^(\s*#|$)' fileNameIGrepFor
也许您还想排除所有空行(仅包含空格)?在这种情况下,更改也很简单:
Maybe you'll also want to exclude all blank lines (only contain whitespace)? In which case, again, the change is simple:
egrep -v '^\s*(#|$)' fileNameIGrepFor
这篇关于grep 行不以 # 或空行开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!