As you comment me: Only what's under [backend], nothing else
It may interest you doing that with Perl and if not just comment me; I will delete and the answer.
Say you have this file:
[no-backend]
# enabled = no
# data source = average
# type = graphite
# destination = localhost
# prefix = netdata
# hostname = localhost
# update every = 10
# buffer on failures = 10
# timeout ms = 20000
[backend]
# enabled = no
# data source = average
# type = graphite
# destination = localhost
# prefix = netdata
# hostname = localhost
# update every = 10
# buffer on failures = 10
# timeout ms = 20000
[no-backend]
# enabled = no
# data source = average
# type = graphite
# destination = localhost
# prefix = netdata
# hostname = localhost
# update every = 10
# buffer on failures = 10
# timeout ms = 20000
This one-liner finds that section for you:
perl -lne '$b=$.; $e=($b+10) if /\[backend\]/;print if $b++<$e' file
or readable version
perl -lne 'if( /\[backend\]/ ){ $b=$.; $e=( $b+10 ); }; if( $b++ < $e ){ print }' file
and the output:
[backend]
# enabled = no
# data source = average
# type = graphite
# destination = localhost
# prefix = netdata
# hostname = localhost
# update every = 10
# buffer on failures = 10
# timeout ms = 20000
and now instead of print you can modify that section with:
s/#//;s/no/yes/;s/(?<=destination = ).+$/192.168.99.38:2003/;s/(?<=hostname = ).+$/$HOSTNAME/
the full one-liner
perl -lpe 'if(/\[backend\]/){$b=$.;$e=($b+10);};if($b++<$e){ s/#//;s/no/yes/;s/(?<=destination = ).+$/192.168.99.38:2003/;s/(?<=hostname = ).+$/\$HOSTNAME/ }' file
and the output:
[no-backend]
# enabled = no
# data source = average
# type = graphite
# destination = localhost
# prefix = netdata
# hostname = localhost
# update every = 10
# buffer on failures = 10
# timeout ms = 20000
[backend]
enabled = yes
data source = average
type = graphite
destination = 192.168.99.38:2003
prefix = netdata
hostname = $HOSTNAME
update every = 10
buffer on failures = 10
timeout ms = 20000
[no-backend]
# enabled = no
# data source = average
# type = graphite
# destination = localhost
# prefix = netdata
# hostname = localhost
# update every = 10
# buffer on failures = 10
# timeout ms = 20000
and finally after checking the output if everything was good, then you can use -i
option to use edit-in-place feature, like:
perl -i.bak -lne '...the rest of the script...' file
.bak is just for getting backup of your old file. ( like: file.txt.bak )
UPDATE for your comment
perl -lpe '$hn=qx(cat /etc/hostname);chomp $hn;if(/\[backend\]/){$b=$.;$e=($b+10);};if($b++<$e){s/#//;s/no/yes/;s/(?<=destination = ).+$/192.168.99.38:2003/;s/(?<=hostname = ).+$/$hn/ }' file
and the output:
...
...
[backend]
enabled = yes
data source = average
type = graphite
destination = 192.168.99.38:2003
prefix = netdata
hostname = k-five
update every = 10
buffer on failures = 10
timeout ms = 20000
...
...