How does Shell perform regular replacement?

for example, I have a string variable that reads:

AAA=<?=\${AAA}=?>
BBB=<?=\${BBB}=?>
<%=[ "\${Alias}" == "" ]=%>
CCC=\${CCC}

I want to implement regular substitution, generating the string as

AAA=<?=${AAA}=?>
BBB=<?=${BBB}=?>
<%= [ "${Alias}" == "" ] =%>
CCC=\${CCC}

or

AAA=${AAA}
BBB=${BBB}
[ "${Alias}" == "" ]
CCC=\${CCC}

how to achieve it? I tried sed, etc., but the ability of Shell is limited, so it is difficult to achieve the effect.

Note: I want to achieve the effect of template engine replacement.


because the question has been corrected, the answer is also updated:
you can solve your problem with grep and sed

echo 'AAA=<?=\${AAA}=?>' | grep -E '<[?%]=.*=[?%]>' | sed 's/\\\$/$/g' | sed 's/<[?%]=//g' | sed 's/=[?%]>//g'

above, return AAA=$ {AAA} . Similarly, the above statement applies to <% = ["\ ${Alias}" = "] =% > .

Menu