0

I have a log file and I want to write a python script to parse out information from that log to another .txt file. I get stuck on how to start because I am very new to python. Could anyone shred some light on how I should do it

My log file contain:

06 May 19 03:40:35 3 abCodeClearTrap Error Clear Trap (agent: 12367a12, chassis:12367a12, ErrIdText: ERROR ID TEXT, csssi: EXTIFG, clearedID: 0x089088394)
06 May 19 03:44:35 3 abCodeErrorTrap Error Trap (agent: 12368a15, chassis: 12368a15, ErrIdText: Skip this item, csssi: SSRSSR, clearedID: 0x089088394)

Say the user want to parse the date, time, agent, Clear, and ErrIDText. How would I go about doing that. Thanks in advance

1
  • The most robust way to parse strings would be the re module. A simpler solution that would easily mess up or break would be to .split() the strings and then refer to items by their index. Commented May 8, 2019 at 21:16

2 Answers 2

3

The full answer to your question is a bit too much, but I can guide you a bit. You should read about regex, which you can use with the re module from python. So, I'll only parse dates from your strings:

import re
string='06 May 19 03:40:35 3 abCodeClearTrap Error Clear Trap (agent: 12367a12, chassis:12367a12, ErrIdText: ERROR ID TEXT, csssi: EXTIFG, clearedID: 0x089088394)\n\
06 May 19 03:44:35 3 abCodeErrorTrap Error Trap (agent: 12368a15, chassis: 12368a15, ErrIdText: Skip this item, csssi: SSRSSR, clearedID: 0x089088394)'

split_error=string.split('\n')

dates=[]
for error in split_error:
    date=re.match('\d{2} \w+ \d{2}', error).group(0)
    dates.append(date)

You could do this even more efficiently with list comprehensions, but if you're new to Python it's best to use more readable code.

I splitted every line of error into a list, and then searched for a combination of two numbers('\d{2}'), a space (), a word character or more ('\w+'), a space (), and then two numbers ( '\d{2}') in each string and extracted that from it. Then I appended the date to the dates list. You can find similar patterns to extract most of the data you're looking for.

Good luck !

Edit: as suggested by @Reedinationer, a good link to get to know patterns after reading a bit about the re module is the regex cheat sheet, I always go there when I forget how to do some regex

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, as I said re would probably be what OP is looking for. If OP wants to learn more, they should click the link in my comment on their question. If you have a better link, Juan it would be great to include it in your post.
2

Here's a rough example how you can open your log file with open() and parse some values from it using the re module and str.split():

import re

with open('myfile.log') as f:
    lines = f.readlines()

data = []
for line in lines:

    date = re.match(r'\d{2} \w+ \d{2}', line).group(0)
    time = line.split()[3]
    agent = re.search(r'agent:\s(.*?),', line).group(1)        
    errID = re.search(r'ErrIdText:\s(.*?),', line).group(1)
    clear = re.search(r'clearedID:\s(.*?)\)', line).group(1)

    row = [date, time, agent, errID, clear]
    data.append(row)

for row in data:
    print(row)

Output:

['06 May 19', '03:40:35', '12367a12', 'ERROR ID TEXT', '0x089088394']
['06 May 19', '03:44:35', '12368a15', 'Skip this item', '0x089088394']

5 Comments

say if I want to get the value "3" between time and abcCode. will I just do "ids = line.split()[1]"? thanks
@NickGonzales That would be ids = line.split()[4]. You should read up more on split from the documentation :)
When I execute the code. I got the error "date=re.match(r'/\d{2} \w+ \d{2}', line).group(0)....AttributeError: 'NoneType' object has no attribute 'group'
@NickGonzales You have an extra / on the start of that regex pattern. In general you can check for the match before calling group(0) on it to avoid errors. e.g. match = re.match(pattern, string); if match: date = match.group(0)
That was my typo on the comment. I did not have that extra "/" in my code and I still have the error. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.