Python Forum
python- read specific words from log file
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python- read specific words from log file
#11
Hi squenson,

1) Open log file (test.log)
2) Read line by line
3) match pattern for MAC and username i.e 0200400681@ttml and 5c:f4:ab:1f:3a:ec in each line
4) print and matches... i.e.
expecting below output

0200400681@ttml 5c:f4:ab:1f:3a:ec
0220367961@ttml 00:17:7c:75:b6:05
Reply
#12
below is the working code, however I am facing difficulty to print in desired format.
this code prints output in below format

0221470118
cc:5d:4e:72:71:a3
4010053642
e8:cc:18:60:f7:cc
0200408496
54:b8:0a:98:70:68
7911007200
74:da:da:d2:76:f7
1201005426
c4:e9:84:97:f4:cd

however how can i print this like

0221470118 cc:5d:4e:72:71:a3
4010053642 e8:cc:18:60:f7:cc
0200408496 54:b8:0a:98:70:68
7911007200 74:da:da:d2:76:f7
1201005426 c4:e9:84:97:f4:cd


#!/usr/bin/python
import re
file = open("test.txt", "r")
mac = re.compile(r'[a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}|\d{10}')
#dn = re.compile(r'(\d{10})')
for line in file:
    mac_address = mac.findall(line)
    for word in mac_address:
        print word
Reply
#13
How does a single line look like?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
@nilamo already provided solution that works. all you need to do is to tweak it accordingly.

data = '''26 Dec 2017 00:00:00,018 [WARN] RAD-AUTH-RES-18 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS ACCEPT, REPLY MESSAGE : Authentication Success, USERNAME : 0221453122@ttml, MAC : ac:f1:df:ea:4c:26, NASIP : 10.124.117.36, NASPORTID : slot=4;subslot=2;port=1;vlanid=3727;vlanid2=1127;
26 Dec 2017 00:00:00,082 [WARN] RAD-AUTH-680 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS REJECT, REPLY MESSAGE : Account is not active, USERNAME : 0220367961@ttml, MAC : 00:17:7c:75:b6:05, NASIP : 10.124.117.36, NASPORTID : slot=5;subslot=2;port=1;vlanid=2000;vlanid2=2488;
26 Dec 2017 00:00:00,115 [WARN] RAD-AUTH-RES-18 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS ACCEPT, REPLY MESSAGE : Authentication Success, USERNAME : 4010059315, MAC : 08:60:6e:c9:dd:9a, NASIP : 10.124.113.246, NASPORTID : slot=6;subslot=2;port=100;vlanid=560;vlanid2=1284;
26 Dec 2017 00:00:00,189 [WARN] RAD-AUTH-695 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS REJECT, REPLY MESSAGE : Account is not active, USERNAME : 0200400681@ttml, MAC : 5c:f4:ab:1f:3a:ec, NASIP : 10.124.117.180, NASPORTID : pppoe lag-50:2925.3643'''
data = data.split('\n')


import csv

def parse_line(line):
    line = [item.split(' : ')[-2:] for item in line]
    return {key.split(':')[-1].strip(): value for key, value in line}


reader = csv.reader(data)
for line in reader:
    row_data = parse_line(line[1:])
    print('{USERNAME} {MAC}'.format(**row_data))
Output:
0221453122@ttml ac:f1:df:ea:4c:26 0220367961@ttml 00:17:7c:75:b6:05 4010059315 08:60:6e:c9:dd:9a 0200400681@ttml 5c:f4:ab:1f:3a:ec
parse_line() will return dict (unordered) that has all data (except for time-stamp), e.g.

Output:
{'NASPORTID': 'pppoe lag-50:2925.3643', 'REPLY MESSAGE': 'Account is not active', 'RESPONSE': 'ACCESS REJECT', 'MAC': '5c:f4:ab:1f:3a:ec', 'USERNAME': '0200400681@ttml', 'NASIP': '10.124.117.180', 'PACKET TYPE': 'ACCESS REQUEST'}
I will leave to you to make it work with your file.
Using RegEx is also possible, but let's keep it simple
Reply
#15
sample two lines
bold highlighted need to print

26 Dec 2017 00:00:00,018 [WARN] RAD-AUTH-RES-18 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS ACCEPT, REPLY MESSAGE : Authentication Success, USERNAME : 0221453122@ttml, MAC : ac:f1:df:ea:4c:26, NASIP : 10.124.117.36, NASPORTID : slot=4;subslot=2;port=1;vlanid=3727;vlanid2=1127;
26 Dec 2017 00:00:00,082 [WARN] RAD-AUTH-680 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS REJECT, REPLY MESSAGE : Account is not active, USERNAME : 0220367961@ttml, MAC : 00:17:7c:75:b6:05, NASIP : 10.124.117.36, NASPORTID : slot=5;subslot=2;port=1;vlanid=2000;vlanid2=2488;
Reply
#16
Perhaps mac_address contains a tuple like ('0221470118', 'cc:5d:4e:72:71:a3')?

for line in file:
    mac_address = mac.findall(line)
    if mac_address
        print "%s %s" % (mac_address[0], mac_address[1])
:D I had some troubles with the print statement. Python 2... Brrrrr
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#17
you can construct better regex and also use named groups

data = '''26 Dec 2017 00:00:00,018 [WARN] RAD-AUTH-RES-18 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS ACCEPT, REPLY MESSAGE : Authentication Success, USERNAME : 0221453122@ttml, MAC : ac:f1:df:ea:4c:26, NASIP : 10.124.117.36, NASPORTID : slot=4;subslot=2;port=1;vlanid=3727;vlanid2=1127;
26 Dec 2017 00:00:00,082 [WARN] RAD-AUTH-680 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS REJECT, REPLY MESSAGE : Account is not active, USERNAME : 0220367961@ttml, MAC : 00:17:7c:75:b6:05, NASIP : 10.124.117.36, NASPORTID : slot=5;subslot=2;port=1;vlanid=2000;vlanid2=2488;
26 Dec 2017 00:00:00,115 [WARN] RAD-AUTH-RES-18 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS ACCEPT, REPLY MESSAGE : Authentication Success, USERNAME : 4010059315, MAC : 08:60:6e:c9:dd:9a, NASIP : 10.124.113.246, NASPORTID : slot=6;subslot=2;port=100;vlanid=560;vlanid2=1284;
26 Dec 2017 00:00:00,189 [WARN] RAD-AUTH-695 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS REJECT, REPLY MESSAGE : Account is not active, USERNAME : 0200400681@ttml, MAC : 5c:f4:ab:1f:3a:ec, NASIP : 10.124.117.180, NASPORTID : pppoe lag-50:2925.3643'''



import re

regex = r"USERNAME : (?P<username>[\d\D@][^ ]*), MAC : (?P<mac>[a-z\d:]*)"
matches = re.finditer(regex, data)
for match in matches:
    my_data = match.group('username', 'mac')
    print(' '.join(my_data))
Output:
0221453122@ttml ac:f1:df:ea:4c:26 0220367961@ttml 00:17:7c:75:b6:05 4010059315 08:60:6e:c9:dd:9a 0200400681@ttml 5c:f4:ab:1f:3a:ec
here is nice regex test site
https://regex101.com/
you can have regex pattern simplified as r"USERNAME : (?P<username>\S*), MAC : (?P<mac>[\S]{17})"
Reply
#18
Thanks Buran.... was struggling to solve this issue, once again you help me. Thanks Ton.

possible to add

REPLY MESSAGE : Account is not active ... messages

pattern in RegEX
Reply
#19
I already provided solution in my post#14
I will leave to you to amend the regex if you would like to use that approach
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,144 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 1,276 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,605 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,814 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,346 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Extracting specific file from an archive tester_V 4 2,287 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Recommended way to read/create PDF file? Winfried 3 4,838 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,883 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 2,638 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 2,260 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020