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.
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
egrep
command that is the same as grep -E
- Carl Norum 2012-04-05 18:53
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:]]\+'?