0

I have an array filled with values from a python-read .txt file, I want to break the information from that file with only what interests me, which are the un commented values, srv2 and srv3

SCRIPT

# value filter without comments
outfinally = []
outfinally = [re.split(r'\s\s+|\s*#\s*|\b\d+\b',line) for line in output[0].splitlines()]

for line in outfinally:
    print(line)

ARCHIVE

########################################################################
#
#       Licensed Materials
#
#
#       (C) Copyright. All Rights Reserved
#
#
#       ========================================================
#       Module Information:
#
#       DESCRIPTION:
#       ping.file
#
######################################################################
#srv1       300
10.10.10.1  300
srv2        300
srv3        300

OUTPUT

['', '========================================================']
['', 'Module Information:']
['', '']
['', 'DESCRIPTION:']
['', 'ping.file']
['', '']
['srv2\t', '']
['srv3\t', '']

DESERIED OUTPUT

srv2
srv3
6
  • 1
    Why not the line '10.10.10.1 300'? Commented Dec 9, 2019 at 16:17
  • desired output is hostname Commented Dec 9, 2019 at 16:18
  • 1
    So, what are the criteria? A non-empty line that doesn't start with '#' or a digit? Commented Dec 9, 2019 at 16:19
  • Are the server names within this file random, not just srv2 and srv3, would it work? Commented Dec 9, 2019 at 16:20
  • The criteria are the lines that contain only hostname, whatever is beyond that, I wanted to eliminate Commented Dec 9, 2019 at 16:21

1 Answer 1

1

There is no need for a regex here, just keep the non-empty lines starting with a letter, and keep the first word:

test = """
#       ping.file
#
######################################################################

#srv1       300
10.10.10.1  300
srv2        300
srv3        300
srv4   # comments
"""

lines = [line.split()[0] for line in test.splitlines() if line and line[0].isalpha()]
print(lines)
# ['srv2', 'srv3', 'srv4']

Or if you want it like this:

print('\n'.join(lines))

Output:

srv2
srv3
srv4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.