示例文件 /tmp/file
This is line one This is line two This is line three This is line four
示例 1
在匹配行前插入多行内容
解决方案
# sed '/This is line two/iyour text 1\nyour text 2\nyour text 3' /tmp/file This is line one your text 1 your text 2 your text 3 This is line two This is line three This is line four
示例 2
在匹配行后一行添加我们的内容
解决方案
# sed '/This is line two/ayour text 1\nyour text 2\nyour text 3' /tmp/file This is line one This is line two your text 1 your text 2 your text 3 This is line three This is line four
sed替换时如何直接修改文件
这可以使用带有 sed 命令的“-i”标志来完成,如下所示。
# sed -i '/This is line two/ayour text 1\nyour text 2\nyour text 3' /tmp/file
建议使用“i.bak”,在替换之前备份目标文件
# sed -i.bak '/This is line two/ayour text 1nyour text 2nyour text 3' /tmp/file
日期:2020-06-02 22:17:33 来源:oir作者:oir