shell - sed:在基础和更新部分添加排除=postgres*

标签 shell sed centos6

/etc/yum.repos.d/CentOS-Base.repo 文件

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#released updates 
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0

我要添加

使用 sed 在 [base] 部分下的 gpgkey 行之后排除 = postgres* 表达式

sed '/^gpgkey/{s/.*/&\nexclude = postgres*/;:a;n;ba}' /etc/yum.repos.d/CetnOS-Base.repo

不更改文件,我收到错误

sed: -e expression #1, char 16: unterminated `s' command

运行此命令,我是否缺少开关或表达式需要更正?

最佳答案

您似乎正在使用 BSD sed。你可以说:

sed '/\[base\]/,/gpgkey=/{/gpgkey=/s/$/\'$'\n''exclude = postgres*/;}' filename

追加行 exclude = postgres*之后gpgkey=...[base]部分。

编辑:说明:

  • /\[base\]/,/gpgkey=/匹配地址范围,即从包含 [base] 的行开始直到包含 gpgkey= 的那个
  • {/gpgkey=/s/$/\'$'\n''exclude = postgres*/;}是一组仅针对上面匹配的地址执行的命令

故障/gpgkey=/s/$/\'$'\n''exclude = postgres*/ :

  • 这将执行上述替换 s/$/\'$'\n''exclude = postgres*/在匹配 gpgkey= 的行上.
  • 以上替换匹配 $ ,即行尾。
  • 替换为换行符,后跟 exclude = postgres*/
  • $'\n'是用于生成换行符的 ANSI-C 引用语法。由于您似乎没有使用 GNU sed,因此这是必需的。否则\n就足够了,即它可以写成 /gpgkey=/s/$/\nexclude = postgres*/

关于shell - sed:在基础和更新部分添加排除=postgres*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22932381/

相关文章:

linux - 在命令提示符下工作正常,但在 shell 脚本中则不行

shell - 在命令行上将密码传递给 curl

python - 启动 shell 脚本不产生输出

linux - 如何根据第 6 个字段内容 grep 行

php - 无法为 symfony 安装 php-xml

mysql - MySQL CentOS 6 无法设置根密码

shell - 在 if 语句中使用 grep

linux - 用多个尾随模式替换行

linux - 如何将列/(conky 命令)向上移动 1 行 - 最好使用 sed

linux - 恢复系统目录权限