Reputation: 69
I'm trying to identify the country abbreviations in a text.
egrep "^[A-Z]{2}$" file
seems to work. But when I'm trying the regex on sed, it's not working. This is what I'm using
sed 's/^[A-Z]{2}$/someCountry/' file
What am I doing wrong?
Upvotes: 1
Views: 135
Reputation: 54475
POSIX sed
uses BREs (basic regular expressions). In those, you can use the equivalent of {2}
, but you must escape the braces. egrep
supports EREs (extended regular expressions). As a side-effect of extending the BREs, the rules for escaping are different (to make the resulting expressions simpler to write).
For reference:
When a BRE matching a single character, a subexpression, or a back-reference is followed by an interval expression of the format
"\{m\}"
,"\{m,\}"
, or"\{m,n\}"
, together with that interval expression it shall match what repeated consecutive occurrences of the BRE would match. The values of m and n are decimal integers in the range0 <= m<= n<= {RE_DUP_MAX
}, where m specifies the exact or minimum number of occurrences and n specifies the maximum number of occurrences. The expression"\{m\}"
shall match exactly m occurrences of the preceding BRE,"\{m,\}"
shall match at least m occurrences, and"\{m,n\}"
shall match any number of occurrences between m and n, inclusive.
Upvotes: 2
Reputation: 36013
man sed
and search for "extended". egrep
is grep
with a flag (-e
on some platforms) for 'extended' regexes, and sed has a similar flag. Otherwise the syntax is different and you have to use \
a lot more.
Upvotes: 1