行范围内的替换 [英] Substitution on range of lines

查看:41
本文介绍了行范围内的替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件的第三行有 .我想用 my_dB 替换它.如何使用 sed 做到这一点.我也想在第 4、5、6 行做这样的替换.

I have <host></host> in 3rd line of a file. I want to replace that with <host>my_dB</host>. How to do this with sed. I want to do replacements like this in 4th, 5th and 6th line too.

<host></host>
<host>my_dB</host>

推荐答案

每个都可以用类似的东西来完成:

Each can be done with something similar to this:

sed -i "s/<host><\/host>/<host>my_db<\/host>/" foo.txt

-i 意味着它将就地覆盖文件.删除该标志以将更改写入标准输出.

The -i means that it'll overwrite the file in-place. Remove that flag to have the changes written to stdout.

如果这是为文件填写详细信息的常规任务,请考虑向文件添加锚点以使其更容易.例如:

If this is for a regular task to fill in details for the file, consider adding anchors to the file to make it easier. For example:

<host>DB_HOST</host>

那么 sed 只需要:

Then the sed would just need to be:

sed -i 's/DB_HOST/my_db/'

这篇关于行范围内的替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆