对于下面的文本,我需要计算括号之间的行数在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 log.txt | grep -c "This function's cyclomatic complexity is too high. ([10-20])"
cat log.txt | grep -c "This function's cyclomatic complexity is too high. ([10,11,12,13,14,15,16,17,18,20])"但是没有起作用。做这件事的正确方法是什么?
发布于 2017-11-16 20:58:06
grep -c "(1[0-9])\|(20)" filename发布于 2017-11-16 20:54:59
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这取决于你的实际数据。
发布于 2017-11-16 20:56:33
我不能访问终端,所以这是未经测试的,但请尝试cat pruebascript.txt | grep -c "This function's cyclomatic complexity is too high. (1[0-9]|20)"。grep中的正则表达式要求1或2,以及2或0。
10-9将匹配10到19以及20之间的任何值。
阅读有关数字范围正则表达式的更多信息:https://www.regular-expressions.info/numericranges.html
https://stackoverflow.com/questions/47330069
复制相似问题