【问题标题】:sed: add exclude=postgres* under base and updates sectionsed:在基础和更新部分添加 exclude=postgres*
【发布时间】:2014-05-20 20:59:11
【问题描述】:

/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

我要添加

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

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

不改变文件,我得到错误

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

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

【问题讨论】:

  • GNU sed 上没有错误,即使使用 --posix

标签: shell sed centos6


【解决方案1】:

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

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

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

编辑:解释:

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

崩溃/gpgkey=/s/$/\'$'\n''exclude = postgres*/

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

【讨论】:

  • sed '/\[base\]/,/gpgkey=/{/gpgkey=/s/$/\\'$'\\n''exclude = postgres*/;}' / etc/yum.repos.d/CentOS-Base.repo,执行没有错误,但文件没有变化。我在 puppet manifest 的 exec 资源中使用它
  • @AnadiMisra 您需要传递-i 选项才能对文件进行就地更改。 sed -i ...
  • 啊!谢谢,你能解释一下第二个 \'$' 是什么吗?据我所知,在你找到 gpgkey = 任何带有 exclude = postgres* 的字符串之后,我们说的是 append,类似的东西:-)
  • @AnadiMisra 在上面添加了解释!
【解决方案2】:

使用 sed

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

【讨论】:

    【解决方案3】:

    你可以用这个sed

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

    【讨论】:

      【解决方案4】:

      在单独的 -e 块中使用 } 尝试这个内联 sed:

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

      【讨论】:

        猜你喜欢
        • 2022-11-11
        • 1970-01-01
        • 2015-06-07
        • 1970-01-01
        • 1970-01-01
        • 2015-07-06
        • 2023-01-29
        • 2013-10-22
        • 1970-01-01
        相关资源
        最近更新 更多