0
  1. echo "123456xx111"| sed '{s/\([x]\)/{\1}/}'
    123456{x}x111
    
  2. echo "123456xx111"| sed '{s/\([x]+\)/{\1}/}'
    123456xx111
    

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
  • 47,233