Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding a string in a regex matching, for sed processing

Tags:

regex

sed

I need to match this for a substitute command:

whatever__MATCH_THIS__whateverwhatever__AND_THIS__whateverwhatever

I am trying with:

sed -e 's/__\(.*\)__/\{{\1}}/g' myfile

But this is eagerly matching __MATCH_THIS__whateverwhatever__AND_THIS__, producing:

whatever{{MATCH_THIS__whateverwhatever__AND_THIS}}whateverwhatever

But I wanted:

whatever{{MATCH_THIS}}whateverwhatever{{AND_THIS}}whateverwhatever

How can I specify a string to exclude, in the matching part? I know how to exclude one character (for example [^a]) but not how to exclude a string.

like image 380
blueFast Avatar asked Mar 23 '23 18:03

blueFast


1 Answers

GNU sed

sed ':k s/__/{{/;s/__/}}/;tk' file

input:

whatever__MATCH_THIS__whateverwhatever__AND_THIS__whateverwhatever
blah__XXX_XX__blah_blah_blah__XX_XXX__whateverwhatever

output:

whatever{{MATCH_THIS}}whateverwhatever{{AND_THIS}}whateverwhatever
blah{{XXX_XX}}blah_blah_blah{{XX_XXX}}whateverwhatever
like image 88
Endoro Avatar answered Apr 07 '23 23:04

Endoro