为什么这些模式 return 结果相同?

Why these patterns return same result?

我看到了这个问题:count (non-blank) lines-of-code in bash

我明白这个模式是正确的。

grep -vc ^$ filename

为什么这个模式 returns 结果相同?

grep -c '[^ ]' filename

'[^ ]'中的技巧是什么?

$ printf 'foo 123\n  \nxyz\n\t\n' > ip.txt
$ cat -T ip.txt
foo 123
  
xyz
^I

$ grep -vc '^$' ip.txt
4
$ grep -c '[^ ]' ip.txt
3
$ grep -c '[^[:blank:]]' ip.txt
2

grep -c '[^ ]' 计算任何具有非 space 字符的行。例如,foo 123 将被计算在内,因为字母表不是 space 个字符。所以,使用哪一个取决于只包含 space 个字符的行是否应该被计算在内。