SED:在第一场比赛后追加

时间:2013-09-25 08:25:03

标签: sed

有没有办法在第一次匹配后仅用sed附加文字?我有类似的东西,但每隔一行插入一次文字:

sed -e '0,/priority/a\exclude = php*' /etc/yum.repos.d/epel.repo

文件

$ cat /etc/yum.repos.d/epel.repo 
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
priority=3

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=1
priority=3

[epel-source]
name=Extra Packages for Enterprise Linux 6 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=1
priority=3

更新

所需的输出 - exclude = php*应仅在第一个回购([epel])中:

$ cat /etc/yum.repos.d/epel.repo 
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
priority=3
exclude = php*
...

3 个答案:

答案 0 :(得分:4)

你可以说:

sed '/^priority/{s/.*/&\nexclude = php*/;:a;n;ba}' /etc/yum.repos.d/epel.repo

答案 1 :(得分:2)

awk

之外的sed解决方案
awk '/^priority/ && !f {$0=$0 RS "nexclude = php*";f=1}1' file

如果找到了搜索并f=0,则添加文字并设置f=1。这样可以防止在所有priority之后添加文字。

答案 2 :(得分:1)

sed  '/priority/{x;/1/{x;b};s/^/1/;x;s/.*/&\nexclude = php*/}' file

上述内容可能适合您。