简体   繁体   中英

Compare All Adjacent Elements of Array

I have a log file that includes different Mac address in different lines.

I can extract the lines that includes given Mac address and then I can trim the line to get only timestamp(such as 15:48:55) then I add this timestamps to an array.

What I want is;

How can I compare all adjacent elements of array if their timestamp subtraction is bigger than 2 seconds?

Log file example:

info: 02-03-2018, 15:48:55.730, 192.168.1.4, 33826, 5C-CF-7F-29-AB-73, 22496
info: 02-03-2018, 15:48:55.894, 192.168.1.6, 17948, A0-20-A6-0A-F2-AB, 22475
info: 02-03-2018, 15:48:56.031, 192.168.1.3, 32538, A0-20-A6-0A-2F-D5, 22510
info: 02-03-2018, 15:48:56.742, 192.168.1.7, 40596, 60-01-94-16-05-96, 22490
info: 02-03-2018, 15:48:57.475, 192.168.1.5, 30646, 5C-CF-7F-DA-5B-77, 22668
info: 02-03-2018, 15:48:57.780, 192.168.1.4, 39592, 5C-CF-7F-29-AB-73, 22497
info: 02-03-2018, 15:48:57.922, 192.168.1.6, 21467, A0-20-A6-0A-F2-AB, 22476
info: 02-03-2018, 15:48:58.055, 192.168.1.3, 13001, A0-20-A6-0A-2F-D5, 22511
info: 02-03-2018, 15:48:58.760, 192.168.1.7, 31030, 60-01-94-16-05-96, 22491
info: 02-03-2018, 15:48:59.487, 192.168.1.5, 46505, 5C-CF-7F-DA-5B-77, 22669

What I got so far:

from datetime import datetime
import os
import re

# Regex used to match relevant loglines (in this case, a specific MAC address)
line_regex = re.compile(r'A0-20-A6-0A-F2-AB')

with open("info.log", "r") as in_file:

    # Loop over each log line
    for line in in_file:

        # If log line matches our regex
        if (line_regex.search(line)):

            #Extract the timestamp as 15:48:55
            asd = line[18:26]

            #Convert to datetime_object
            datetime_object = datetime.strptime(asd, '%H:%M:%S')

            #Trim begining of the datetime object
            dsa = datetime_object.strftime ('%H:%M:%S')

            #Add to an array as a timestamp
            for j in range(1):
                    array1=[]
                    for i in range(1):
                        array1.append(dsa)
                        print array1

You don't really need extra modules for this, you just need to use the datetime object, not a string.

from datetime import datetime
import os
import re

line_regex = re.compile(r'A0-20-A6-0A-F2-AB')

prev_dsa = None
with open("info.log", "r") as in_file:
    for line in in_file:
        if (line_regex.search(line)):
            asd = line[6:26]
            dsa = datetime.strptime(asd, '%m-%d-%Y, %H:%M:%S')
            if (prev_dsa != None):
                if abs((dsa - prev_dsa).seconds) >= 2:
                    print dsa
            prev_dsa = dsa

You can use pandas for this. Note: this can easily be adapted to take date into account, but as it stands it only takes into account the time component.

df = pd.read_csv('file.csv', header=None, delimiter=', ')
df['Diff'] = pd.to_datetime(df[1], format='%H:%M:%S.%f').diff().dt.microseconds / 10**6

# greater than half a second differences
res = df[df['Diff'] > 0.5]

#                   0             1            2      3                  4  \
# 3  info: 02-03-2018  15:48:56.742  192.168.1.7  40596  60-01-94-16-05-96   
# 4  info: 02-03-2018  15:48:57.475  192.168.1.5  30646  5C-CF-7F-DA-5B-77   
# 8  info: 02-03-2018  15:48:58.760  192.168.1.7  31030  60-01-94-16-05-96   
# 9  info: 02-03-2018  15:48:59.487  192.168.1.5  46505  5C-CF-7F-DA-5B-77   

#        5   Diff  
# 3  22490  0.711  
# 4  22668  0.733  
# 8  22491  0.705  
# 9  22669  0.727  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM