4

I want to edit manually my repo file from the command-line, preferably using sed. How can I do that based on the repo-name I want to edit?

I want to search for a specific repo-name (example: reponame-2) and based on that change, for example, the option enabled=1 to enabled=0

[repo-name1]
name=repo-name1
baseurl=http://linktomyrepo.com
enabled=1
sslverify=0
proxy=_none_


[repo-name2]
name=repo-name2
baseurl=http://linktomyrepo.com
enabled=1
sslverify=0
proxy=_none_
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Asaf Magen
  • 487
  • 1
  • 9
  • 21

3 Answers3

5

Perl's "paragraph mode", where "lines" are defined by consecutive newlines, is perfect for this:

$ perl -00pe 's/enabled=1/enabled=0/ if /\[repo-name1/' file [repo-name1]
name=repo-name1
baseurl=http://linktomyrepo.com
enabled=0
sslverify=0
proxy=_none_


[repo-name2]
name=repo-name2
baseurl=http://linktomyrepo.com
enabled=1
sslverify=0
proxy=_none_

Or, to edit the original file directly:

perl -i -00pe 's/enabled=1/enabled=0/ if /\[repo-name1/' file 

Alternatively, you could use awk:

$ awk -vRS='\n\n' -vORS='\n\n' '/\[repo-name1/{sub(/enabled=1/,"enabled=0")}1;' file 
[repo-name1]
name=repo-name1
baseurl=http://linktomyrepo.com
enabled=0
sslverify=0
proxy=_none_

[repo-name2]
name=repo-name2
baseurl=http://linktomyrepo.com
enabled=1
sslverify=0
proxy=_none_

And, if you have a recent version of GNU-awk or any other awk suporting -i, you can do this to edit in place:

awk -iinplace -vRS='\n\n' -vORS='\n\n' '/\[repo-name1/{sub(/enabled=1/,"enabled=0")}1;' file 

Alternatively, to avoid the extra blank lines that the awk above adds to the end of the file, you could do something more complex like:

$ awk -F= '/\[repo-name1/{a=1}/^\s*$/{a=0}a==1 && $1=="enabled"{$2=0}1;' file
[repo-name1]
name=repo-name1
baseurl=http://linktomyrepo.com
enabled 0
sslverify=0
proxy=_none_

[repo-name2]
name=repo-name2
baseurl=http://linktomyrepo.com
enabled=1
sslverify=0
proxy=_none_
terdon
  • 234,489
  • 66
  • 447
  • 667
  • works very well. it would be nice if i could use sed or awk. because im not sure all my systems has perl installed by default. – Asaf Magen Jun 15 '16 at 17:30
  • 1
    @AsafMagen the vast majority of *nix systems will have Perl installed. Granted, [not all](http://unix.stackexchange.com/a/123149/22222), so I added ann `awk` solution as well. – terdon Jun 15 '16 at 17:52
2

Something like this:

sed -i '/repo-name1/{n;n;n;s/enabled=1/enabled=0/}' repos.txt

This requires that enabled= always be at the third line after repo name.

Vombat
  • 12,654
  • 13
  • 44
  • 58
2

With awk one can set record separator to double newlines to in some way simulate perl's paragraph mode as in terdon's answer:

awk 'BEGIN{RS="\n\n";OFS="\n"} \
     /repo-name1/{for(i=1;i<=NF;i++) if($i=="enabled=1") $i="enabled=0"}1' file
jimmij
  • 46,064
  • 19
  • 123
  • 136
  • You might also want to set `ORS` so that the blank lines are kept in the output. – terdon Jun 15 '16 at 17:54
  • @terdon yes, I see that you added your `awk` solution with this, but one will end up with blank lines also at the end of the file. `awk` is not best for this job, perl is better. – jimmij Jun 15 '16 at 17:59
  • Never mind, I added another approach that avoids the newlines. – terdon Jun 15 '16 at 18:03