I have a Windows-10 machine, on which I've installed a Ubuntu platform, as can be seen in following uname -a result:

Linux DOMINIQUEDS 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 x86_64 x86_64 x86_64 GNU/Linux

I have some files which contain lots of entries like:

18bd6344        mfc110u!CPtrArray        Size:[1046]
18bd63f4        mfc110u!CPtrArray        Size:[1046]
18bd64a4        mfc110u!CPtrArray        Size:[1046]
18bdcef4        mfc110u!CPtrArray        Size:[4]
18bdcfa4        mfc110u!CPtrArray        Size:[8]
18bdd054        mfc110u!CPtrArray        Size:[49]
18bdd104        mfc110u!CPtrArray        Size:[498]
18bdd1b4        mfc110u!CPtrArray        Size:[498]

The mentioned sizes vary from 1 to many millions, and I'm interested in the larger ones, let's say the ones who contain at least three digits.

I can do this using following regular expression:

grep "Size:\[[0-9][0-9][0-9]" Log1.log // this is working fine

I expected following regular expression to give the same result:

grep "Size:\[[0-9]{3,}" Log1.log

But I get nothing.

I've just verified man grep, and this explanation contains the following:

{n,} The preceding item is matched n or more times.

So why is this not working?

  • 2
    grep "Size:\[[0-9]\{3,\}" Log1.log – RomanPerekhrest Jan 19 '18 at 9:07
  • @RomanPerekhrest: please put this as an answer, I'll confirm it immediately. It works as a charm :-) – Dominique Jan 19 '18 at 9:14

Since it's on Ubuntu, I'm guessing you have GNU grep. The man page also says:

grep understands three different versions of regular expression syntax: “basic” (BRE), “extended” (ERE) and “perl” (PCRE). In GNU grep, there is no difference in available functionality between basic and extended syntaxes. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards.

And then:

Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).

So, you need to either use \{3,\}, or use the -E flag to enable EREs. I'd suggest the latter, since those backslashes everywhere get ugly rather quickly.

  • grep -E "Size:\[[0-9]{3,}" is workin fine indeed, and it looks better than the solution with the backslashes. :-) – Dominique Jan 19 '18 at 9:32

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.