-
echo "123456xx111"| sed '{s/\([x]\)/{\1}/}' 123456{x}x111 -
echo "123456xx111"| sed '{s/\([x]+\)/{\1}/}' 123456xx111
Asked
Active
Viewed 62 times
0
Stéphane Chazelas
- 544,893
user2358299
- 21
1 Answers
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
{and}commands). – Toby Speight Jan 07 '22 at 14:49