-2

i need to replace with sed command this lines:

;[homes]
;   browseable = no

In this lines:

[homes]
browseable = yes

4 Answers4

2

sed -e 's/^; *//', unless your example doesn't cover all cases.

fstd
  • 574
  • 2
  • 7
2

There are lots of ways to fix the two lines shown. It gets a little trickier if you consider that this is ini-file format and there could be other sections which contain browseable and other sections than [homes] which should not be edited.

sed -e '/^;\[homes]/,/^;*\[/{
        s/;\[homes]/[homes]/
        s/^; *browseable.*/browseable = yes/
        }'

Given the input file:

;[alerts]
;  browseable = no
;[homes]
;  browseable = no
;  alternatives = no
;[houses]
;  browseable = no
[mezzanine]
;  browseable = no
   alternatives = yes

The output from the above script is:

;[alerts]
;  browseable = no
[homes]
browseable = yes
;  alternatives = no
;[houses]
;  browseable = no
[mezzanine]
;  browseable = no
   alternatives = yes
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

You can use awk to do the changes.

awk '{sub(/^;[ ]*/,"")} /browseable/ {$3="yes"}1'
[homes]
browseable = yes

Given data from Jonthan

awk '/homes/ {sub(/^;[ ]*/,"");f=1} f && /browseable/ {sub(/^;[ ]*/,"");$3="yes";f=0}1' file
;[alerts]
;  browseable = no
[homes]
browseable = yes
;  alternatives = no
;[houses]
;  browseable = no
[mezzanine]
;  browseable = no
   alternatives = yes

Or like this:

awk '/homes/ {$0="[homes]";f=1} f && /browseable/ {$0="browseable = yes";f=0}1' file
Jotne
  • 40,548
  • 12
  • 51
  • 55
0

This will also work,

sed 's/^;//g;s/^ *\(.*\)/\1/g;s/no/yes/g' file
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274