I am trying to figure out how to grab the number of clients on each of the AP lines from this text file, which appears to be fine for the ones that have a 'Num of clients' associated with it (on the next line), but you can see if no clients are associated to it, it doesn't print a num of clients.
I cant figure out the logic to check the next line to see if 'num of clients' is present, and then go back to the current line. If its present, it should proceed to the next line and grab the client number. If there is no 'num of clients' line, I'm trying to set the clients to 0.
I have a file that consists of the following:
wireless-detail.txt
RUDY>show wireless ap detail on AP1 | include clients
RUDY>show wireless ap detail on AP2 | include clients
Num of clients : 8
RUDY>show wireless ap detail on AP3 | include clients
Num of clients : 21
RUDY>show wireless ap detail on AP4 | include clients
RUDY>show wireless ap detail on AP5 | include clients
Num of clients : 2
Right now I have the following:
for line in file:
if "AP" in line:
ap = re.search('AP[0-99]+', line)
print ap.group(0),
elif "Num of clients" in line:
clients = re.search('[0-99]+', line)
print '- ' + clients.group(0)
It currently prints the following:
AP1 AP2 - 8
AP3 - 21
AP4 AP5 - 2
AP6 - 5
AP7 - 2
AP8 - 5
AP9 - 5
What is the best method to have it check the next line to see if the AP should be set to 0 clients or not?
Edit: FWIW - I was trying file.next() to read the next line, which seems to work, but I couldn't go back to the previous line :/
Edit #2: I wish I could upvote you all. Thanks everyone for this! Incredible how there are so many ways to do it and I couldn't figure out 1 of them!!!!