I wrote this regex to match numbers greater than or equal 3600. This is my attempt. However, I am not sure if it is complete:

grep -P '36[0-9]+[0-9]+[0-9]*' test.txt

I mean positive decimal integer numbers only (I do not need to consider floating, negative numbers, octal, hexadecimal, roman numerals...).

  • 1
    you're missing everything 3700 or greater, for starters! or is there an upper limit? – Jeff Schaller May 16 '19 at 13:02
  • 1
    Do you want to exclude a number like 3600.0? – Jeff Schaller May 16 '19 at 13:10
  • No floats, negatives or any kind of numbers mentioned by @Stéphane Chazelas. Just simple integers 2600, 3601, 3602, etc. They appear anywhere on the line. – user9371654 May 16 '19 at 15:38
  • @Jeff Schaller No upper limit. Can you propose a fix? – user9371654 May 16 '19 at 15:45
  • 7
    Regular expressions are really the wrong hammer for this job. – glenn jackman May 16 '19 at 19:57

The extended regular expression ([1-9][0-9]{2,}|[4-9][0-9]|3[6-9])[0-9]{2} should do the job if you can be sure there are no negative numbers, floats, thousand limiters and so on around

The expression has three paths, all of which have in common the last part [0-9]{2}, which means two digits.

  • First path is a 1 to 9 with at least two more digits ([0-9]{2,}) and those common twi digits, so it's all numbers with five or more digits: 10000 and above
  • Second path is a 4 to 9 with three more digits: 4000 to 9999
  • Third path is a 3 and something from 6 to 9 and those two digits. This matches everything between 3600 and 3999
  • But you limit the number's length. How about this number: 31536000 or this: 31536000000?? Can you please explain your regex a bit? – user9371654 May 16 '19 at 15:41
  • It does not capture this: 36000 although it is greater than 3600. – user9371654 May 16 '19 at 15:44
  • Those examples are covered in the third path. Explanation added – Philippos May 16 '19 at 16:42
  • I have tested it in 36000. It does not match the last zero. Only matches 3600 part. May be te last part in you regex should be [0-9]{2,} instead of [0-9]{2}?? – user9371654 May 16 '19 at 18:11
  • You'll want to add anchors: \<(...)\> – glenn jackman May 16 '19 at 19:59

Regex is not good comparing numbers !

Better use some scripting language. In your case awk would do a good job:

awk -F '[^0-9]*' '{for(i=1;i<=NF;i++){ if (int($i)>3600) { print; next; } }}' test.txt

Dependend on your input you should adapt this a bit.
My short example would e.g. not work correctly with negative numbers.


Using grep

for n in $(grep -Po "[0-9]+" test); do if [ $n -ge 3600 ]; then echo $n; fi; done

If your file only contains integers, because it will report 5999 against 0.5999

Or without the if

grep -Po "[1-9][0-9]{4,}|[4-9][0-9]{3}|3[6-9][0-9]{2}" test


Stole the leading digit strategy [1-9] on 5 digits from @Phillippos above. Answers are basically the same though I find this more readable


If the numbers are separated by blanks (or on each line) this regex will print only (-o) the matching number(s) (no leading zeros accepted):

grep -wE '[1-9][0-9]{4,}|(3[6-9]|[4-9][0-9])[0-9]{2}'

If the numbers could start with one or more zeros, then use this:

grep -wE '0*([1-9][0-9]{4,}|(3[6-9]|[4-9][0-9])[0-9]{2})'

Or, using PCRE:

grep -wP '0*([1-9]\d{4,}|(3[6-9]|[4-9]\d)\d{2})'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.