简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM