I am trying to write a script which will go through text file and check for particular content and assign to the variable.
For example:
Text file content:
eth0 Link encap:Ethernet HWaddr 08:ee:27:ff:b3:d7
inet addr:10.0.2.45 Bcast:10.3.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe00:b3d7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:16178 errors:0 dropped:0 overruns:0 frame:0
TX packets:8559 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14045795 (14.0 MB) TX bytes:1355632 (1.3 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:666 errors:0 dropped:0 overruns:0 frame:0
TX packets:666 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:72748 (72.7 KB) TX bytes:72748 (72.7 KB)
I would like to check value of 'RX packets' on interface eth0 and assign value '16178' to the variable. I need to be able to extract this value from this particular block 'eth0'.
Please advise where to start?
Thank you.
You can use regular expression to extract the value.
Try a pattern:
m = re.match("\W*RX packets[^:]*:(\d+)", line)
In a regular expression \d
means a digit, and a +
means one or more. You want to 'match' one or more numbers in the text. The brackets mean to capture what is found into a number, this number should be found after a specific text RX packets:
.
You can find more details about regular expression in the official doc.
Your code would look like:
data= """
eth0 Link encap:Ethernet HWaddr 08:ee:27:ff:b3:d7
inet addr:10.0.2.45 Bcast:10.3.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe00:b3d7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:16178 errors:0 dropped:0 overruns:0 frame:0
TX packets:8559 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14045795 (14.0 MB) TX bytes:1355632 (1.3 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:666 errors:0 dropped:0 overruns:0 frame:0
TX packets:666 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:72748 (72.7 KB) TX bytes:72748 (72.7 KB)"""
import re
def findSeq(block,data):
isInRightBlock= False
for line in data.splitlines():
if block in line:
isInRightBlock= True
m = re.match("\W*RX packets[^:]*:(\d+)", line)
if m and isInRightBlock:
isInRightBlock= False
return m.group(1)
res= findSeq("eth0",data)
print res #Your Value
Output:
16178
Banchemark
from datetime import datetime
start_time_1 = datetime.now()
res= findSeq("eth0",data)
print('Duration: {}'.format(datetime.now() - start_time_1))
from datetime import datetime
start_time_2 = datetime.now()
re.search(r'eth0.*?RX packets:(\d+)',data,re.DOTALL).group(1)
print('Duration: {}'.format(datetime.now() - start_time_2))
Output:
Duration: 0:00:00.000547
Duration: 0:00:00.000344
NT: You can optimize the way it checks the right block.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments