Python : search two regular expression in one command -
how search 2 regular expressions in 1 findall
command? have ugly program done, learn better python way this.
in following example, identify servers either (alarm1 side0 & alarm1 side1) or (problem 2).
file :
server 1, side 0, alarm 1 server 1, side 1, alarm 1 server 1, problem 2 server 2, alarm 2 server 3, side 0, alarm 1 server 4, side 1, alarm 1 server 5, alarm 3
expected output :
server 1, alarm 1, alarm 2 server 2, alarm 2
script :
#! /usr/bin/python import re import sys import os def main(): server_alarm_list=[] open('sample_data') f: text=f.read() alarm1_side0_tuple=re.findall(r'server (\d+), side 0, alarm 1', text) alarm1_side1_tuple=re.findall(r'server (\d+), side 1, alarm 1', text) alarm2_tuple=re.findall(r'server (\d+), problem 2', text) in alarm1_side0_tuple: if in alarm1_side1_tuple: server_alarm_list.append(i) in alarm2_tuple: if in server_alarm_list: print "server" + + ", alarm 1, alarm 2" else: print "server" + + ", alarm2" if __name__ == '__main__': main()
Comments
Post a Comment