Reputation: 1291
I have text:
abc abc Abc ABC AB_C
I want to match words with capital letters and dash( this is not obligatory).
My solution is:
[A-Z]+(_{0,1}[A-Z]+)+
And it works on regexpal.com but it doesn't work with sed
. What am I doing wrong?
sed 's/\([A-Z]+(_{0,1}[A-Z]+)+\)/\1/g'
Upvotes: 3
Views: 402
Reputation: 16507
You can do it with two ways:
With sed filtering lower-case letters:
$ echo 'abc abc Abc ABC AB_C' | sed "s/\s/\n/g" | sed '/[a-z]/d'
ABC
AB_C
$ echo 'abc abc Abc ABC AB_C' | sed "s/\s/\n/g" | grep "^[A-Z][A-Z_]*$"
ABC
AB_C
Upvotes: 0
Reputation: 195109
by default sed
uses BRE. which means, you have to escape the chars with special meaning, like + ( ...
. to "give" them special meaning.
if you are using gnu sed, you can use -r
option to make sed use ERE
.
Hope this is helpful.
Upvotes: 1
Reputation: 785276
That regex isn't supported in traditional sed. You can use grep -oP
(with PCRE flag)
s='abc abc Abc ABC AB_C'
grep -oP '([A-Z]+(_?[A-Z]+)+)' <<< "$s"
ABC
AB_C
Upvotes: 4