Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Comment whole section from ini file

Instead of using the ansible ini_file state=absent section="Zabbix", you can use this instead, to comment out the section. I realize you can just do a backup=yes option, but for the quick cases where you don't want to fire up ansible:

tmpfile1=$( mktemp ); sed -r -e '/^\[/i#ENGLISH877' ~/foo | sed -r -e '/^\[Zabbix\]/,/ENGLISH877/s/^/#/;' | sed -r -e '/#ENGLISH877/d' > ${tmpfile1}; cat ${tmpfile1} > ~/foo

Explanation

The first sed statement inserts a new line with content "#ENGLISH877" (random string that will be unlikely to cause collisions) right before the start of each new section. The second sed statement then modifies the section specified ("Zabbix") and inserts comments at the beginning. The last sed removes all instances of the random string we inserted.

Comments