为什么这些模式返回相同的结果?

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

我理解这种模式是正确的。

grep -vc ^$ filename

为什么这个模式返回相同的结果?

grep -c '[^ ]' filename

'[^ ]' 中有什么技巧?

chendy0899 回答:为什么这些模式返回相同的结果?

$ 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 '[^ ]' 计算任何具有非空格字符的行。例如,foo 123 将被计算在内,因为字母不是空格字符。因此,使用哪一个取决于是否应计算仅包含空格字符的行。

本文链接:https://www.f2er.com/6217.html

大家都在问