The following sed command seems to work:
sed 's/{{\([^*[a-z][^}]*\)}}/{{\\\1}}/g;s/{{\\${\([^}]*\)}}}/{{$\\{\1\\}}}/g'
Explanation:
{{\([^*[a-z][^}]*\)}}
finds{}
, except whenstuff
begins with*
or[
or a lower-case letter.- Replace it with
{{\stuff}}
. - Then
{{\\${\([^}]*\)}}}
finds{{\$}}
. - And replaces it with
{{$\}}
.
Edit: An alternative solution, after clarification from the OP, could be this:
sed 's/\({{[^}]*\){\([^}]*}}\)/\1\\{\2/g;s/\({{[^}]*\)}}}/\1\\}}}/g'
As we all know, sed cannot do recursive parsing, but this should work for most simple cases.
Explanation:
\({{[^}]*\){\([^}]*}}\)
finds{}
, wherefoo
andbar
do not contain}
.- And replaces it with
{}
. (Note{}}
is handled ok.) - Then
\({{[^}]*\)}}}
finds{}}
, wherebaz
does not contain}
. - And replaces it with
{}}
.
foo
, bar
, and baz
can be empty, so for example {{}}}
is converted to {{\}}}
, as required.