Try augeas. It offers both Python bindings and a command-line interface - augtool.
[dba@pgsql ~]$ augtool
augtool> defvar pghba /files/var/lib/pgsql/data/pg_hba.conf
augtool> ####################################
augtool> ls $pghba
#comment[1] = this is a comment
1/ = (none)
2/ = (none)
#comment[2] = this another comment
3/ = (none)
augtool> ####################################
augtool> ls $pghba/1
type = local
database = all
user = all
method = trust
augtool> ####################################
augtool> insert 01 after $pghba/2
augtool> set $pghba/01/type host
augtool> set $pghba/01/database db1
augtool> set $pghba/01/user user1
augtool> set $pghba/01/address 127.0.0.1
augtool> set $pghba/01/method md5
augtool> ####################################
augtool> save
Saved 1 file(s)
(Comments start with #
)
I think this should be asked on the Software Recommendations SE.
– Cristian Ciupitu – 2014-06-21T00:53:30.317@@Karl Richter, if you're happy with my answer, you can still accept it even if the question is closed. – Cristian Ciupitu – 2014-06-23T08:41:39.510