对于以下文字,我需要计算括号之间的数字在[10-20]范围内的行。
This function's cyclomatic complexity is too high. (1)
This function's cyclomatic complexity is too high. (2)
This function's cyclomatic complexity is too high. (3)
This function's cyclomatic complexity is too high. (4)
This function's cyclomatic complexity is too high. (5)
This function's cyclomatic complexity is too high. (6)
This function's cyclomatic complexity is too high. (7)
This function's cyclomatic complexity is too high. (8)
This function's cyclomatic complexity is too high. (9)
This function's cyclomatic complexity is too high. (10)
This function's cyclomatic complexity is too high. (12)
This function's cyclomatic complexity is too high. (13)
This function's cyclomatic complexity is too high. (14)
This function's cyclomatic complexity is too high. (15)
This function's cyclomatic complexity is too high. (16)
This function's cyclomatic complexity is too high. (17)
我尝试以下命令:
cat pruebascript.txt | grep -c "This function's cyclomatic complexity is too high. ([10-20])"
cat pruebascript.txt | grep -c "This function's cyclomatic complexity is too high. ([10,11,12,13,14,15,16,17,18,20])"
但没有奏效。这是正确的方法吗?
答案 0 :(得分:3)
grep -c "(1[0-9])\|(20)" filename
答案 1 :(得分:1)
grep
是错误的工具。您不希望使用正则表达式进行数值比较。尝试:
awk "/This function's cyclomatic complexity is too high./"'$2 >= 10 && $2 <=20 {c++} END {print c}' FS='[()]' pruebascript.txt
请注意,您可以简化为:
awk '$2 >= 10 && $2 <=20 {c++} END {print c}' FS='[()]' pruebascript.txt
取决于您的实际数据。
答案 2 :(得分:1)
我无法访问终端,因此未经测试,请尝试cat pruebascript.txt | grep -c "This function's cyclomatic complexity is too high. (1[0-9]|20)"
。您在grep中的正则表达式要求1或2,以及2或0。
1 [0-9]将匹配10到19之间以及20之间的任何内容。
阅读有关数值范围正则表达式的更多信息: https://www.regular-expressions.info/numericranges.html
答案 3 :(得分:1)
要列出grep
的不同字符串,请将其\(\)
包围,并与\|
分开,如下所示:
$ grep "This function's cyclomatic complexity is too high. (\(10\|11\|12\))" file
This function's cyclomatic complexity is too high. (10)
This function's cyclomatic complexity is too high. (12)