0

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?

4

1 に答える 1

1

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.

于 2018-03-20T15:05:03.043 に答える