[Tutor] Python Regex re.search() to parse system logs

Mike Wilbur wilbur6453 at gmail.com
Mon Dec 21 17:48:24 EST 2020


I am beginning to learn python regex.  I was presented with the following
problem:

*Given:*
import re
def show_time_of_pid(line):
  pattern = ___  # need to complete regex code to support desired output
  result = re.search(pattern, line)
  return ___  # need to complete for desired output

print(show_time_of_pid("Jul 6 14:01:23 computer.name CRON[29440]: USER
(good_user)")) # call to run function with parameter
# Desired output per below:
# Jul 6 14:01:23 pid:29440

My code so far keeps pulling in the string "computer.name CRON[".  I can
get the date & time OR the pid #.  But not by themselves.  I have not
looked at adding the "pid:" to the output yet.

*My code:*
print(re.search("(^[\w \:]{15}.*[^a-z\.CRON][0-9]{5})", "Jul 6 14:01:23
computer.name CRON[29440]: USER (good_user)"))
<re.Match object; span=(0, 39), match='Jul 6 14:01:23 computer.name
CRON[29440'>

Produced code using group names that isolates desired output.  But this
will not work with re.search() I believe.  I think I'd need to use re.sub()
instead.


*print(re.search("(?P<timestamp>[\w
\:]{15}).*[^a-zCRON].*\[(?P<pid>[\d]{5})\]", "Jul 6 14:01:23 computer.name
<http://computer.name> CRON[29440]: USER (good_user)"))<re.Match object;
span=(0, 40), match='Jul 6 14:01:23 computer.name <http://computer.name>
CRON[29440]'>*

[image: image.png]


I know I am missing something straight forward.  I've been unsuccessful
using google or stackoverflow to find what I am missing.

Any suggestions would be greatly appreciated.

Best, Mike


More information about the Tutor mailing list