python - Use regex to process log file: combine regex expressions -


i have file contains string output shown in code below. want extract data:

how use regex achieve this? can combine 2 regex expressions using one?

i need ignore "dropped" string well

port: dropped-packets s1-eth1: 21 s1-eth2: 12 ... 

processqdisc.py

output = ''' qdisc pfifo_fast 0: dev eth0 root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1  sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc pfifo_fast 0: dev eth1 root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1  sent 47002846 bytes 549464 pkt (dropped 0, overlimits 0 requeues 1)   backlog 0b 0p requeues 1  qdisc htb 5: dev s2-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 25845 bytes 186 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 24941 bytes 182 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 24043 bytes 177 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 25506 bytes 183 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth6 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 25173 bytes 182 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 26996 bytes 197 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 26793 bytes 197 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28350 bytes 206 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 26856 bytes 195 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth6 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 27651 bytes 199 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28908 bytes 208 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28654 bytes 205 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28494 bytes 203 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 27723 bytes 198 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth6 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28996 bytes 206 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth7 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28586 bytes 201 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth8 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28494 bytes 203 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth9 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28836 bytes 204 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth10 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28334 bytes 201 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth11 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28654 bytes 205 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28586 bytes 201 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28404 bytes 202 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28334 bytes 201 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28154 bytes 199 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth6 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28244 bytes 200 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 4149 bytes 29 pkt (dropped 3, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc netem 10: dev s2-eth1 parent 5:1 limit 1000 loss 10%  sent 4149 bytes 29 pkt (dropped 3, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 21723 bytes 160 pkt (dropped 21, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc netem 10: dev s1-eth1 parent 5:1 limit 1000 loss 10%  sent 21723 bytes 160 pkt (dropped 21, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 4942 bytes 36 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 23646 bytes 173 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 6025 bytes 52 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 23018 bytes 160 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s5-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 4002 bytes 24 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 24586 bytes 185 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 5587 bytes 43 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 23720 bytes 169 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  ''' 

my code:

import re searchobj = re.findall( r's\d-eth\d', output, re.m|re.i) searchobj2 = re.findall( r'dropped \d*', output, re.m|re.i)  print searchobj print searchobj2 

output getting:

['s2-eth2', 's2-eth3', 's2-eth4', 's2-eth5', 's2-eth6', 's3-eth2', 's3-eth3', 's3-eth4', 's3-eth5', 's3-eth6', 's4-eth2', 's4-eth3', 's4-eth4', 's4-eth5', 's4-eth6', 's4-eth7', 's4-eth8', 's4-eth9', 's4-eth1', 's4-eth1', 's6-eth2', 's6-eth3', 's6-eth4', 's6-eth5', 's6-eth6', 's2-eth1', 's2-eth1', 's1-eth1', 's1-eth1', 's3-eth1', 's1-eth2', 's4-eth1', 's1-eth3', 's5-eth1', 's1-eth4', 's6-eth1', 's1-eth5'] ['dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 3', 'dropped 3', 'dropped 21', 'dropped 21', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0', 'dropped 0'] 

combining lists can 1 option

#processqdisc.py import re  output = ''' qdisc pfifo_fast 0: dev eth0 root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1  sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc pfifo_fast 0: dev eth1 root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1  sent 47002846 bytes 549464 pkt (dropped 0, overlimits 0 requeues 1)   backlog 0b 0p requeues 1  qdisc htb 5: dev s2-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 25845 bytes 186 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 24941 bytes 182 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 24043 bytes 177 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 25506 bytes 183 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth6 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 25173 bytes 182 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 26996 bytes 197 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 26793 bytes 197 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28350 bytes 206 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 26856 bytes 195 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth6 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 27651 bytes 199 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28908 bytes 208 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28654 bytes 205 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28494 bytes 203 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 27723 bytes 198 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth6 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28996 bytes 206 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth7 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28586 bytes 201 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth8 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28494 bytes 203 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth9 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28836 bytes 204 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth10 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28334 bytes 201 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth11 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28654 bytes 205 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28586 bytes 201 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28404 bytes 202 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28334 bytes 201 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28154 bytes 199 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth6 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 28244 bytes 200 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s2-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 4149 bytes 29 pkt (dropped 3, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc netem 10: dev s2-eth1 parent 5:1 limit 1000 loss 10%  sent 4149 bytes 29 pkt (dropped 3, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 21723 bytes 160 pkt (dropped 21, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc netem 10: dev s1-eth1 parent 5:1 limit 1000 loss 10%  sent 21723 bytes 160 pkt (dropped 21, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s3-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 4942 bytes 36 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth2 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 23646 bytes 173 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s4-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 6025 bytes 52 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth3 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 23018 bytes 160 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s5-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 4002 bytes 24 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth4 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 24586 bytes 185 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s6-eth1 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 5587 bytes 43 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0  qdisc htb 5: dev s1-eth5 root refcnt 2 r2q 10 default 1 direct_packets_stat 0 direct_qlen 1000  sent 23720 bytes 169 pkt (dropped 0, overlimits 0 requeues 0)   backlog 0b 0p requeues 0   ''' searchobj = re.findall( r's\d-eth\d', output, re.m|re.i) searchobj2 = re.findall( r'dropped \d*', output, re.m|re.i)  # print searchobj # print searchobj2 print {i:j in searchobj j in searchobj2} 

output:

{'s1-eth2': 'dropped 0', 's4-eth2': 'dropped 0', 's4-eth3': 'dropped 0', 's4-eth1': 'dropped 0', 's4-eth6': 'dropped 0', 's4-eth7': 'dropped 0', 's4-eth4': 'dropped 0', 's4-eth5': 'dropped 0', 's4-eth8': 'dropped 0', 's4-eth9': 'dropped 0', 's1-eth5': 'dropped 0', 's1-eth4': 'dropped 0', 's5-eth1': 'dropped 0', 's1-eth1': 'dropped 0', 's1-eth3': 'dropped 0', 's3-eth6': 'dropped 0', 's3-eth5': 'dropped 0', 's3-eth4': 'dropped 0', 's3-eth3': 'dropped 0', 's3-eth2': 'dropped 0', 's3-eth1': 'dropped 0', 's2-eth1': 'dropped 0', 's2-eth2': 'dropped 0', 's2-eth3': 'dropped 0', 's2-eth4': 'dropped 0', 's2-eth5': 'dropped 0', 's2-eth6': 'dropped 0', 's6-eth4': 'dropped 0', 's6-eth5': 'dropped 0', 's6-eth6': 'dropped 0', 's6-eth1': 'dropped 0', 's6-eth2': 'dropped 0', 's6-eth3': 'dropped 0'} 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -