【发布时间】:2019-07-18 03:07:42
【问题描述】:
我需要使用 perl 或 sed 等命令在 /etc/xinetd.d/chargen 中设置 disable=no。
/etc/xinetd.d/chargen内容是:
# description: An xinetd internal service which generate characters. The
# xinetd internal service which continuously generates characters until the
# connection is dropped. The characters look something like this:
# !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg
# This is the tcp version.
service chargen
{
disable = yes
type = INTERNAL
id = chargen-stream
socket_type = stream
protocol = tcp
user = root
wait = no
}
# This is the udp version.
service chargen
{
disable = yes
type = INTERNAL
id = chargen-dgram
socket_type = dgram
protocol = udp
user = root
wait = yes
}
我用过perl命令
perl -0777 -pe 's|(service chargen[^\^]+)disable\s+=\syes|\1disable=no|' /etc/xinetd.d/chargen
但它只在一个地方替换。
# description: An xinetd internal service which generate characters. The
# xinetd internal service which continuously generates characters until the
# connection is dropped. The characters look something like this:
# !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg
# This is the tcp version.
service chargen
{
disable = yes
type = INTERNAL
id = chargen-stream
socket_type = stream
protocol = tcp
user = root
wait = no
}
# This is the udp version.
service chargen
{
disable=no
type = INTERNAL
id = chargen-dgram
socket_type = dgram
protocol = udp
user = root
wait = yes
}
什么是让它在这两个地方都工作的正确命令?
注意:我可以用 disable = no 替换 disable = yes 而不匹配 service chargen 但我需要使用相同的 sed/perl 命令替换 /etc/xinetd.conf 中的其他服务.
更新正如乔纳森在他的评论中强调的那样,禁用可以在花括号内的任何位置。
【问题讨论】:
-
您的替换文本中有错字
disbale而不是disable。 -
将
[^\^]+更改为.*?并在第二个|之后添加sg标志。并将\1替换为$1。
标签: regex perl awk sed ubuntu-16.04