GNU regex in grep working different on redhat vs Windows

Go To StackoverFlow.com

0

I am trying to grep for any string that matches the following:

AAA-###

Where A is any alpha character, and there can be from 2 to 5 of them.

# is any digit, and there could be 1 or more of them.

So, the following strings should be found:

ABC-123
DE-4
FGHI-56789

But this shouldn't be found:

A15-B432

I tried a few different things, like this:

grep [[:alpha:]]\{2,5\}-[[:digit:]]\+

However, that didn't work.

This is gnu grep on redhat. In my testing, using grep on my windows machine, which was also using gnu grep, this worked. Both seem to be version 2.5.1.

2012-04-05 18:28
by jgritty


2

You need the -E flag for extended regular expressions:

$ cat file
ABC-123
DE-4
FGHI-56789
A15-B432
$ grep [[:alpha:]]\{2,5\}-[[:digit:]]\+ file 
$ grep -E [[:alpha:]]\{2,5\}-[[:digit:]]\+ file 
ABC-123
DE-4
FGHI-56789

I personally prefer to use '' to avoid all the \ characters:

$ grep -E '[[:alpha:]]{2,5}-[[:digit:]]+' file 
ABC-123
DE-4
FGHI-56789
2012-04-05 18:45
by Carl Norum
Both your examples worked perfectly - jgritty 2012-04-05 18:52
No problem - there's usually an egrep command that is the same as grep -E - Carl Norum 2012-04-05 18:53


0

Do you know which part is not working?

Is it the [:alpha:] and [:digit:], or the \{2,5\} or just the fact that you have not enclosed the regexp in quotes

grep '[[:alpha:]]\{2,5\}-[[:digit:]]\+'?
2012-04-05 18:45
by barsju
Ads