首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >grep数字范围

grep数字范围
EN

Stack Overflow用户
提问于 2017-11-16 20:48:13
回答 4查看 7.8K关注 0票数 4

对于下面的文本,我需要计算括号之间的行数在10-20之间。

代码语言:javascript
复制
    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)

我正在尝试以下命令:

代码语言:javascript
复制
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])"

但是没有起作用。做这件事的正确方法是什么?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-11-16 20:58:06

代码语言:javascript
复制
grep -c "(1[0-9])\|(20)" filename
票数 3
EN

Stack Overflow用户

发布于 2017-11-16 20:54:59

grep是错误的工具。您不希望使用正则表达式进行数值比较。尝试:

代码语言:javascript
复制
awk "/This function's cyclomatic complexity is too high./"'$2 >= 10 && $2 <=20 {c++} END {print c}' FS='[()]' pruebascript.txt

请注意,您可能可以简化为:

代码语言:javascript
复制
awk '$2 >= 10 && $2 <=20 {c++} END {print c}' FS='[()]' pruebascript.txt

这取决于你的实际数据。

票数 1
EN

Stack Overflow用户

发布于 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47330069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档