示例文件:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
# <docker-registry>
docker.local 127.0.0.1
# </docker-registry>
使用sed(https://unix.stackexchange.com/a/236754)的范围表达式功能,我想删除以下几行:
# <docker-registry>
docker.local 127.0.0.1
# </docker-registry>
在尝试了不同的语法之后,这是我能想到的最好的方法:
sed -n 's|# <docker-registry>|,|# </docker-registry>|p' $FILE
没有s
标志,出现以下错误:
sed: 1: "|# <docker-registry>|,| ...": invalid command code |
使用s
标志,出现以下错误:
sed: 1: "s|# <docker-registry>|, ...": bad flag in substitute command: '#'
如何获得所需的结果?
我在发布问题时找到了答案,所以出于后验:
d
标志:sed -n 's|# <docker-registry>|,|# </docker-registry>|d' $FILE
sed -n 's|# <docker-registry>|,|# </docker-registry>|d' $FILE > $FILE
sed: 1: "s|# <docker-registry>|, ...": bad flag in substitute command: '#'
错误:sed '/# <docker-registry>/,/# <\/docker-registry>/d' $FILE > $FILE
你去了:)
您将在标准输出上收到结果:
sed -e '/# <docker-registry>/,/# <\/docker-registry>/d' yourdatafile
如果需要编辑数据文件,请使用ed而不是sed。编剧:
/# <docker-registry>/,/# <\/docker-registry>/d
w
q
电话:
ed yourdatafile <script.ed