Capital letter Sed does not work in regex group

I have a text:

abc abc ABC ABC AB_C

I want to combine words with capital letters and dashes (this is not necessary).

My decision:

[A-Z]+(_{0,1}[A-Z]+)+

And it works on regexpal.com , but it does not work with sed. What am I doing wrong?

sed 's/\([A-Z]+(_{0,1}[A-Z]+)+\)/\1/g'
+3
source share
3 answers

This regular expression is not 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
+4
source

seduses BRE by default . this means that you need to avoid characters with special meaning, for example + ( .... "attach" them special significance.

gnu sed, -r, sed ERE.

, .

+1

:

  • :

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

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

All Articles