nervosol
nervosol

Reputation: 1291

Sed capital letter doesnt work in regex group

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

Answers (3)

Малъ Скрылевъ
Малъ Скрылевъ

Reputation: 16507

You can do it with two ways:

  1. With filtering lower-case letters:

    $ echo 'abc abc Abc ABC AB_C' | sed "s/\s/\n/g" | sed '/[a-z]/d' 
    ABC
    AB_C
    
  2. With using not the :

    $ echo 'abc abc Abc ABC AB_C' | sed "s/\s/\n/g" | grep "^[A-Z][A-Z_]*$"
    ABC
    AB_C
    

Upvotes: 0

Kent
Kent

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

anubhava
anubhava

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

Related Questions