Nov-08-2018, 09:57 AM
i am trying to layout some code to edit aix stanza files. basically i want to add/remove/edit special formated config values from various text files. in the end this should come out as an working ansible module ;-).
this is the desired format:
i already experimented with regexes but the follwing leaves the file empty.
this is the desired format:
stanza1: value1 = attr1 value2 = attr2 stanza2: value1 = attr1 value3 = attr1 stanza3: value1 = attr1 value1 = attr1i want to search for the stanza and only add it if it does not exists. a newline is needed above each stanza to separate it from the above. the value/attr pairs should have a "tab' in front of them. it should be possibe to remove/add value/attr pairs to stanzas and removal of the whole stanza (incl. related value/attrs).
i already experimented with regexes but the follwing leaves the file empty.
#!/usr/bin/env python import re with open('stanza_test', 'r+') as fh: text = fh.read() if re.search(r'(?m)^shittyjohn:', text): print('Found.') else: print('Not found.') pattern = r'^((?:[^\n]+\n){%d})' fh.seek(0) fh.write( re.sub(pattern, r'\1shittyjohn:\n', text) )any hints,food for thought,ideas, code snippets would be of great help, as i am little bit stuck with this.