0
  1. echo "123456xx111"| sed '{s/\([x]\)/{\1}/}'
    123456{x}x111
    
  2. echo "123456xx111"| sed '{s/\([x]+\)/{\1}/}'
    123456xx111
    
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • Possible duplicate of [Why does my regular expression work in X but not in Y?](//unix.stackexchange.com/q/119905) – Stéphane Chazelas Jan 06 '22 at 12:04
  • Is there a question? Those outputs look correct to me (though I believe you're relying on a GNU extension with those unseparated `{` and `}` commands). – Toby Speight Jan 07 '22 at 14:49

1 Answers1

4

Plus needs a backslash if supported at all (it's a GNU extension):

echo "123456xx111"| sed '{s/\([x]\+\)/{\1}/}'
123456{xx}111

Or, switch to extended regular expressions:

echo "123456xx111"| sed -E '{s/([x]+)/{\1}/}'
123456{xx}111

You can simplify your expression a lot, as the outer {} don't do anything (and may not work in some sed implementations without a semicolon or newline before the closing brace); a one element character class is equivalent to the character itself; and when replacing the whole string, you don't have to capture anything:

echo "123456xx111"| sed -E 's/x+/{&}/'
123456{xx}111

or, without -E, without any GNU extensions (so using the quantifier \{1,\} "one or more"):

echo "123456xx111"| sed 's/x\{1,\}/{&}/'
123456{xx}111
choroba
  • 45,735
  • 7
  • 84
  • 110