sed 和 awk : how to replace a section of file to another content?

标签 sed awk

文件a.html:

<!--TEMPLATE: banner-->
blahblah
<!--TEMPLATE-END: banner-->

我想将中间文本替换为其他文本,如何使用 sed/awk/其他工具实现?

最佳答案

如果标记行之间的文本由一行或多行组成:

sed '/<!--TEMPLATE: banner-->/,/<!--TEMPLATE-END: banner-->/ {//!d}; /<!--TEMPLATE: banner-->/aSome text to insert' a.html
i命令可以更改为 r命令和文件名以从文件中读取文本。
sed '/<!--TEMPLATE: banner-->/,/<!--TEMPLATE-END: banner-->/ {//!d}; /<!--TEMPLATE: banner-->/r filename' a.html

关于sed 和 awk : how to replace a section of file to another content?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4311915/

相关文章:

regex - 正则表达式不提取组

awk - 如何使用 sed 选择特定行?

linux - 删除日志文件中的重复行

linux - 如何获取不包含某些模式的行

linux - 替换两列 linux

linux - awk group by 并在符合条件时打印

string - Bash - 如何替换特定索引处的字符(包括索引零)

regex - 在特定模式匹配后插入文件内容

linux - linux中如何在文件中插入一行

bash - 使用 awk 计算接口(interface)类型