我的“测试”文件有3种空行:
$ cat -nA test
1 This is line 1 and it's followed by a blank line$
2 $
3 This is line 2 and it's followed by a 'blank' line with 3 tab characters$
4 ^I^I^I$
5 This is line 3 and it's followed by a 'blank' line with 3 whitespace characters$
6 $
7 This is line 4$
如果我用POSIX字符类“blank”或“space”开头的空行,我会得到以制表符或空格开头的空行:
$ grep -n '^[[:blank:]]' test
4:
6:
$ grep -n '^[[:space:]]' test
4:
6:
如果我反转搜索,我会得到所有其他行:
$ grep -nv '^[[:blank:]]' test
1:This is line 1 and it's followed by a blank line
2:
3:This is line 2 and it's followed by a 'blank' line with 3 tab characters
5:This is line 3 and it's followed by a 'blank' line with 3 whitespace characters
7:This is line 4
$ grep -nv '^[[:space:]]' test
1:This is line 1 and it's followed by a blank line
2:
3:This is line 2 and it's followed by a 'blank' line with 3 tab characters
5:This is line 3 and it's followed by a 'blank' line with 3 whitespace characters
7:This is line 4
然而,如果我否定搜索,我只得到非空行:
$ grep -n '^[^[:blank:]]' test
1:This is line 1 and it's followed by a blank line
3:This is line 2 and it's followed by a 'blank' line with 3 tab characters
5:This is line 3 and it's followed by a 'blank' line with 3 whitespace characters
7:This is line 4
$ grep -n '^[^[:space:]]' test
1:This is line 1 and it's followed by a blank line
3:This is line 2 and it's followed by a 'blank' line with 3 tab characters
5:This is line 3 and it's followed by a 'blank' line with 3 whitespace characters
7:This is line 4
为什么否定否定与反转相同?
答案 0 :(得分:1)
[^[:space:]]
表示“除[:space:]
类之外的任何字符”。因此,匹配^[^[:space:]]
的行必须至少包含一个字符。