sed:在基本和更新部分下添加exclude = postgres * [英] sed: add exclude=postgres* under base and updates section

查看:134
本文介绍了sed:在基本和更新部分下添加exclude = postgres *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

/etc/yum.repos.d/CentOS-Base.repo file

# 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

我要添加

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

exclude = postgres* after gpgkey line under the [base] section using sed, the expression

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

不更改文件,但出现错误

does not change the file, I get error

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

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

running this command, am I missing a switch or the expression needs correction?

推荐答案

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

You seem to be using BSD sed. You can say:

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

中的 gpgkey = ... 之后添加 exclude = postgres * 行[基本] 部分。

to append the line exclude = postgres* after gpgkey=... in the [base] section.

编辑:解释:


  • / \ [base\] /,// gpgkey = / 匹配地址范围,即从包含 [base]的行开始的行直到包含 gpgkey =

  • {/ gpgkey = / s / $ / \'$'\n''exclude = postgres * /;} 是一组命令,仅对上面匹配的地址执行

  • /\[base\]/,/gpgkey=/ matches an addresses range, i.e. lines starting from the one containing [base] upto the one containing gpgkey=
  • {/gpgkey=/s/$/\'$'\n''exclude = postgres*/;} is a group of commands that is executed only for the addresses matched above

破坏 / gpgkey = / s / $ / \'$'\n''exclude = postgres * /


  • 此操作执行上述替换 s / $ / \'$'\n''排除= postgres * / 匹配 gpgkey = 的行。

  • 上述替换匹配 $ ,即en

  • 替换是换行符,后跟 exclude = postgres * /

  • $'\n'是用于产生换行符的ANSI-C引用语法。由于您似乎未使用GNU sed,因此这是必需的。否则 \n 就足够了,即它可以写为 / gpgkey = / s / $ / \nexclude = postgres * /

  • This performs the mentioned substitution s/$/\'$'\n''exclude = postgres*/ on lines that match gpgkey=.
  • The above substitution matches $, i.e. the end of line.
  • The replacement is a newline followed by exclude = postgres*/
  • $'\n' is ANSI-C quoting syntax for producing a newline. Since you didn't seem to be using GNU sed, hence this was required. Else \n would suffice, i.e. it could have been written as /gpgkey=/s/$/\nexclude = postgres*/

这篇关于sed:在基本和更新部分下添加exclude = postgres *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆