Luis Henrique
Luis Henrique

Reputation: 771

Extract values from configuration file with regex

I'm trying to read a configuration file and extract some parameters, these are:

They follow a standard XML format, where the primary key (hostame) is inside <hostname> and below the values ​​that end with <hostname/> My regex seems to work like the example below, however when I read dataCfg don't receive any value.

The print row I put in the code just to see if something is being returned, but the output I want is the commented print lines #

filter.py

#!/usr/bin/env python
# coding=utf8
import re

# Strings regex
regexCfg = r"<(.*)>\n      active = (.*)"

# Get file .cfg
with open("/etc/ansible/files/net_connect.cfg", 'r') as cfgfile:
    dataCfg = cfgfile.readlines()

# Capture values
# Realize regex in string
filterdCfg = re.findall(regexCfg, str(dataCfg), re.MULTILINE)

# Print regex value
print("[RESULT] Row: {}".format(filterdCfg))
#print("Hostname: {}".format(filterdCfg[0][0]))
#print("Status: {}".format(filterdCfg[0][1]))

Output

[RESULT] Row: []

Desired Output

Hostname: SERVENFE
Status: yes
Hostname: SERVENFE2
Status: yes

Data file

   <SERVENFE>
      active = yes
   <SERVENFE>
   <SERVENFE2>
      active = yes
   <SERVENFE2>

Regex example

Upvotes: 0

Views: 237

Answers (1)

wjandrea
wjandrea

Reputation: 33022

The problem is .readlines(). That reads the file into a list of lines. Simply use .read() instead, to read it into one big string. I.e.

dataCfg = cfgfile.read()

Output:

[RESULT] Row: [('SERVENFE', 'yes'), ('SERVENFE2', 'yes')]

However, as mentioned in the comments, parsing is ultimately the better way, though your example is not valid XML.

Upvotes: 1

Related Questions