Python Forum
edit text files/ add lines if missing (regex)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
edit text files/ add lines if missing (regex)
#1
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:

stanza1:
        value1 = attr1
        value2 = attr2

stanza2:
        value1 = attr1
        value3 = attr1

stanza3:
        value1 = attr1
        value1 = attr1
i 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.
Reply
#2
Have you checked to ensure that your regexes work? Can you post an excerpt of the file?
Reply
#3
hmm..i started small with just trying to add a single stanza line in an empty file, with the code snippet in my first post.
when the file is emtpy it just prints "Not found", which is correct, but not adds anything. if i add the stanza line before manually, it just prints "Found". so i guess the regex works "somewhat".
Reply
#4
update: got the first step working, i can now add stanza lines if they dont exist, and avoid duplicates. it's something Shy

#!/usr/bin/env python

import re

config_file="stanza_test"
stanza_name="smittyjohn"

with open(config_file, 'r+') as fh:
    text = fh.read()
    if re.search('(?m)^' + stanza_name + ':', text):
        print('stanza found.')
    else:
        print('stanza not found.')
        fh.seek(0)
        fh.write(
            text + "\n"  + stanza_name + ":\n"
        )
root@lpgaixmgmtlx01:/root>./add_stanza.py
stanza not found.
root@lpgaixmgmtlx01:/root>./add_stanza.py
stanza found.
root@lpgaixmgmtlx01:/root>./add_stanza.py
stanza found.

root@lpgaixmgmtlx01:/root>cat stanza_test

smittyjohn:

shittyjohn:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  azure TTS from text files to mp3s mutantGOD 2 1,695 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  Writing into 2 text files from the same function paul18fr 4 1,671 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,546 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  select files such as text file RolanRoll 2 1,157 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Two text files, want to add a column value zxcv101 8 1,899 Jun-20-2022, 03:06 PM
Last Post: deanhystad
  python-docx regex: replace any word in docx text Tmagpy 4 2,215 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Editing text between two string from different lines Paqqno 1 1,311 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,507 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,951 Mar-28-2022, 03:38 PM
Last Post: snippsat
  trying to edit a text file greatfella 9 2,857 Dec-14-2021, 02:06 PM
Last Post: greatfella

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020