SCO
SCO

Reputation: 1932

Grep not matching a simple pattern

I'm scratching my head on the following oversimplistic grep command:

grep "GMT \+[0-9]{2}:[0-9]{2}" gmt_funny.txt

where gmt_funny.txt contains:

2012-09-01 00:00:16.825 (GMT +02:00)

I've just discovered that the grep command doesn't match the line, unless I specify -E as follows:

grep -E "GMT \+[0-9]{2}:[0-9]{2}" gmt_funny.txt

Does this means grep doesn't handle extended regular expressions ? The man grep seems to indicate that { and } is not supported, shall be replaced by \{ and \}. Is this correct?

If yes, is there an explanation to this misleading particular behaviour of grep?

Upvotes: 0

Views: 303

Answers (1)

nhahtdh
nhahtdh

Reputation: 56809

By default, grep uses BRE syntax, where quantifier syntax \{m,n\} requires backslash \ on the curly brackets. So your first command can be changed to:

grep "GMT \+[0-9]\{2\}:[0-9]\{2\}" gmt_funny.txt

To use the ERE syntax in grep, where quantifier syntax is the familiar {m,n}, you need to specify -E flag in your command, as you have found out in the question.

Upvotes: 1

Related Questions