-2

I would like to capture and replace with sed the following pattern :

{{/tagName}}} to {{/tagName}} }

But I cannot figure it out

I have tried

sed 's/({{\/.*}})/\1 }/g'

But it does not work. It does not capture and the blank space does not work in the 2nd part. Any idea? Thanks

UPDATE : To be more concise, the question is If I have to replace this pattern in a file with the option -i, how should I do ?

 sed -i 's/({{\/.*}})/\1 }/g' ./temp.txt
toch
  • 67
  • 6

2 Answers2

4

Or if you'd like BRE syntax, you could do it too. It would be rare case when they are more compact.

$ sed  's/{{[^{}]\+}}/& /' <<< '{{/tagName}}}'
{{/tagName}} }

& in replacement the string represents full match of a the regexp part.

EDIT: to replace all occurrences if file you can use

sed -i 's/{{[^{}]\+}}/& /g' ./temp.txt
markalex
  • 8,623
  • 2
  • 7
  • 32
3

Like this:

$ sed -E 's/\{\{[^\}]+\}\}/& /' <<< '{{/tagName}}}'
{{/tagName}} }

LESS=+/'^ +-E' man sed

-E, -r, --regexp-extended

& is the matched part

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • Thanks, it works, but I have updated the question. I was not enough clear in my demand. Thank you – toch Apr 17 '23 at 05:51
  • In some cases `-E` reduces the cognitive overload, in this case I would posit it does not. – potong Apr 17 '23 at 09:00