127.0.0.1 localhost
127.0.1.1 exmaple-some.com
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
add-host() {
sed -i "/^# The following/i\\${1}" hosts
}
add-host '127.0.1.1 a.newdomain.com'
add-host '127.0.1.1 b.newdomain.com'
127.0.0.1 localhost
127.0.1.1 exmaple-some.com
127.0.1.1 a.newdomain.com
127.0.1.1 b.newdomain.com
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.0.1 localhost
127.0.1.1 exmaple-some.com
127.0.1.1 a.newdomain.com
127.0.1.1 b.newdomain.com
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
exmaple-some.com 所在行实际内容不定,所以无法匹配这行再往下插入。
sed 一般是对行进行处理,所以无法匹配\n# The following
,查了资料说可以用跳转标记匹配多行,但我一脸懵逼没看懂用法。
1
fangxing204 2017-04-16 18:31:46 +08:00 via Android
sed 是面向“行”的编辑器
|
2
yangg 2017-04-16 19:33:46 +08:00 1
sed 专门干这事的!!!
使用函数 a sed "/match_reg/a added content" -i hosts 参考: https://uedsky.com/2016-10/sed-awk/#sed-tutorial |
4
yangg 2017-04-17 19:51:05 +08:00 via iPhone
不是在行下面插入内容么?什么系统?
|
6
yangg 2017-04-17 21:06:05 +08:00 via iPhone
跨行什么意思,示例没看出来
|