Pattern matching digits does not work in egrep?

user377622 picture user377622 · Jul 6, 2010 · Viewed 37.5k times · Source

Why can't I match the string

"1234567-1234567890"

with the given regular expression

\d{7}-\d{10}

with egrep from the shell like this:

egrep \d{7}-\d{10} file

?

Answer

polygenelubricants picture polygenelubricants · Jul 6, 2010

egrep doesn't recognize \d shorthand for digit character class, so you need to use e.g. [0-9].

Moreover, while it's not absolutely necessary in this case, it's good habit to quote the regex to prevent misinterpretation by the shell. Thus, something like this should work:

egrep '[0-9]{7}-[0-9]{10}' file

See also

References