sed: add exclude=postgres* under base and updates section

為{幸葍}努か 提交于 2020-01-05 04:41:12

问题


/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

I want to add

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?


回答1:


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

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

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

EDIT: Explanation:

  • /\[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

Breaking down /gpgkey=/s/$/\'$'\n''exclude = 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*/



回答2:


Try this inline sed with } in separate -e block:

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



回答3:


You can use this sed,

sed '/\(\[base\]\|\[updates\]\)/,/gpgkey/{/gpgkey/s/.*/&\nexclude=postgres*/}' /etc/yum.repos.d/CetnOS-Base.repo



回答4:


Using sed

sed '/\[base\]/,/gpgkey/{/gpgkey/s/.*/&\nexclude = postgres*/}' /etc/yum.repos.d/CetnOS-Base.repo


来源:https://stackoverflow.com/questions/22932381/sed-add-exclude-postgres-under-base-and-updates-section

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!