1

Is there a way to append a text after first match only with sed? I've got something like this but text is inserted every second line:

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

File

$ 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

UPDATE:

Desired output - exclude = php* should be only in the first repo ([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*
...
4

3 回答 3

4

You can say:

sed '/^priority/{s/.*/&\nexclude = php*/;:a;n;ba}' /etc/yum.repos.d/epel.repo
于 2013-09-25T08:42:41.780 回答
2

An awk solution in addition to sed

awk '/^priority/ && !f {$0=$0 RS "nexclude = php*";f=1}1' file

If search is found and f=0, then add text and set f=1. This prevents adding text after all priority.

于 2013-09-25T09:20:39.573 回答
1
sed  '/priority/{x;/1/{x;b};s/^/1/;x;s/.*/&\nexclude = php*/}' file

the above line may work for you.

于 2013-09-25T09:26:54.553 回答