perl - (Bash) Evaluate text between tokens with command output in the file -


i have text file, example

text text <%any_command "$(another_command)" parameter %> text text

text * text <%any_command "$(another_command)" parameter %> text text

i need evaluata text inside <% %> , replace it(including these tokens result that) result of evaluation.

how e done using akw or sed or perl?

i've tried use sed without success:

sed "s/<%\(.*\)%>/$(eval \1)/g"

-bash: 1: command not found

update:

two additional requirements

1) several commands replace on same line:

text * text <%any_command "$(another_command)" parameter %> text<%any_command "$(another_command)" parameter %>text

2) function placed inside <%%> tags, example have any.sh file following content:

#/bin/bash function any_function { echo $1 }

and line processed:

text * text <% any_function 1 %> text text

don't believe possible single sed command. store result of command in variable, sub in:

while read line;      result=$(echo "$line" | sed -r 's~.*<%(.*)%>.*~\1~' | bash)     echo "$line" | sed -r "s~<%.*%>~$result~" done < input.txt 

this sensitive commands - second sed command won't if command contains newlines, example, because variable gets expanded before command run.

input file:

text text <%head -1 /home/ooh/tmp/script%> text text text * text <%echo "$(date +%y/%m/%d)"%> text text 

output:

text text while read line;  text text text * text 2014/03/24 text text 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -