File a.html:
<!--TEMPLATE: banner-->
blahblah
<!--TEMPLATE-END: banner-->
I want to replace the middle text to some other text, how to achieve that using sed/awk/other tools?
If the text between the marker lines consists of one or more lines:
sed '/<!--TEMPLATE: banner-->/,/<!--TEMPLATE-END: banner-->/ {//!d}; /<!--TEMPLATE: banner-->/aSome text to insert' a.html
The i
command could be changed to an r
command and a file name to read the text from a file.
sed '/<!--TEMPLATE: banner-->/,/<!--TEMPLATE-END: banner-->/ {//!d}; /<!--TEMPLATE: banner-->/r filename' a.html
If you have the new banner text in another file:
awk -v new_file=new_banner.txt '
/!--TEMPLATE:/ {print; system("cat " new_file); banner=1; next}
/!--TEMPLATE-END:/ {banner=0}
banner {next}
{print}
' a.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With