示例文件:
##
# 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: '#'
怎样才能达到预期的效果?
发布于 2020-05-09 16:20:08
您将在stdout上收到结果:
sed -e '/# <docker-registry>/,/# <\/docker-registry>/d' yourdatafile如果需要编辑数据文件,请使用ed而不是sed。不要使用重定向到您正在读取的文件,因为它可能会损坏文件。
编辑脚本:
/# <docker-registry>/,/# <\/docker-registry>/d
w
q打电话:
ed yourdatafile <script.ed发布于 2020-05-09 16:06:21
TL;DR:
sed '/# <docker-registry>/,/# <\/docker-registry>/d' $FILE > "$FILE.tmp"
mv "$FILE.tmp" $FILE解释:
我在发帖的时候找到了答案,所以在事后:
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 '/# <docker-registry>/,/# <\/docker-registry>/d' $FILE > "$FILE.tmp"
mv "$FILE.tmp" $FILE如果你有一个更优雅的答案,可以随意编辑:)
https://stackoverflow.com/questions/61699739
复制相似问题