2017-11-16 9 views
3

次の文章では、括弧間の数字が[10-20]の範囲にある行を数える必要があります。grepの範囲

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])" 

しかしdidntの仕事を。それを行う正しい方法は何ですか?

答えて

3
grep -c "(1[0-9])\|(20)" filename 
+0

ええ、あなたは正しいです。 – tso

1

grepは間違ったツールです。正規表現を使用して数値比較を行うことは望ましくありません。試してみてください:あなたの実際のデータに応じて、

awk '$2 >= 10 && $2 <=20 {c++} END {print c}' FS='[()]' pruebascript.txt 

:あなたはおそらくに簡素化することができ

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

注意を。

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]は19にだけでなく、20

数値範囲の正規表現についての詳細を読む10の間には何もマッチしますhttps://www.regular-expressions.info/numericranges.html

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)