0

I am extacting the output based on the if else elif condition and printing the line starting with pattren | SUCCESS |, however i'm trying to add the underline marks for | SUCCESS | line just to make distinction.

As i'm using a variable UNDERLINE for making that underline, Just curious to Know if there is better way to give that underline.

UNDERLINE = '---------------------------------------------'
def Filter_Suc():
    patt_success = False
    with open("shanghai2_out") as f:
        for line in f:
            if patt_success:
                if "FAILED" in line or "UNREACHABLE" in line:
                    patt_success = False
                else:
                    if "| SUCCESS |" in line:
                        print(UNDERLINE)
                        print(line.rstrip('\n'))
                        print(UNDERLINE)
                    else:
                        print(line.rstrip('\n'))
            elif "| SUCCESS |" in line:
                print(line.rstrip('\n'))
                patt_success = True

Filter_Suc()

The above code works well and producing the below output: However, the output between the underline is okay but as i needed only the Hostname ie that is udalt-chef.preet.com SO, just wondering how we can chop off the | SUCCESS | rc=0 >>

---------------------------------------------------------
udalt-chef.preet.com | SUCCESS | rc=0 >>
---------------------------------------------------------
domain odence.com
search odence.com global.odence.com
nameserver 192.168.2.14
nameserver 192.168.2.15

Sorry for asking too much! However, will appreciate any help in intrim.

krock1516
  • 441
  • 10
  • 30
  • @rahlf23, `strip()`: will remove leading and trailing characters `lstrip()`: will remove leading characters `rstrip()`: will remove trailing characters – krock1516 Mar 13 '18 at 15:02
  • To remove the EOL character from the right side of a string: `line = line.rstrip('\n')` – krock1516 Mar 13 '18 at 15:03

1 Answers1

1

Here is one way to strip the extra stuff after the first appearance of something - ' ' in this case:

>>> line='udalt-chef.preet.com | SUCCESS | rc=0 >>'
>>> print(line[:line.index(' ')])
udalt-chef.preet.com

index will return the first index of the given sub-string, and [:index] returns everything up until that index, effectively chopping off the rest.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • This looks to be good, i was looking at the same https://stackoverflow.com/questions/6266727/python-cut-off-the-last-word-of-a-sentence – krock1516 Mar 13 '18 at 15:25
  • @krock1516 In that question you are looking for the last occurrence of something, here the first. Still, I would use `rfind` there. In your case you just need to replace your `rstrip` with the above. – kabanus Mar 13 '18 at 15:27
  • What does mean when we say `index(' ')` or `' '` specially. – krock1516 Mar 13 '18 at 15:31
  • @krock1516 Like it says in the answer - it returns the index of the first occurrence of the given string (space in this case). It would be the same if you used `line.find(' ')`. – kabanus Mar 13 '18 at 15:32