Home > OS >  Keep only letters and spaces from log file with python
Keep only letters and spaces from log file with python

Time:12-20

I have a log file that looks like this:

kernel: apparmor = "STATUS" operation = "profile_load" profile = "unconfined" name = "nvidia_modprobe" comm = "apparmor_parser"
kernel: audit: apparmor = "STATUS" operation="profile_load" profile="unconfined" name="nvidia_modprobe comm="apparmor_parser"
kernel: audit: apparmor = "STATUS" operation="profile_load" profile = "unconfined" 
kernel: audit: apparmor = "STATUS" operation= "profile_load"

I read as a multiline string and I want to keep only letters and spaces and look like this

kernel apparmor  STATUS operation  profile_load profile  unconfined name  nvidia_modprobe comm  apparmor_parser
kernel audit apparmor  STATUS operation profile_load profile  unconfined name nvidia_modprobe comm apparmor_parser
........

I can do it with

unwanted_chars = ":.,/"
log.replace(unwanted_chars, "")

but I don't want to have to add all possible characters. I was thinking something with isalpha and isspace or some regex.

CodePudding user response:

you could use the re.sub method to use a regular expression for your substitution. This way you can define a negated range expression. I.E replace anything thats not the ranges i have defined. In the code below, it will replace anything thats not an upper case or lower case letter, or a digit from 0 to 9 or a space.

import re


data = '''kernel: apparmor = "STATUS" operation = "profile_load" profile = "unconfined" name = "nvidia_modprobe" comm = "apparmor_parser"
kernel: audit: apparmor = "STATUS" operation="profile_load" profile="unconfined" name="nvidia_modprobe comm="apparmor_parser"
kernel: audit: apparmor = "STATUS" operation="profile_load" profile = "unconfined" 
kernel: audit: apparmor = "STATUS" operation= "profile_load"'''

print(re.sub(r"[^A-Za-z0-9\s]", "", data))

OUTPUT

kernel apparmor  STATUS operation  profileload profile  unconfined name  nvidiamodprobe comm  apparmorparser
kernel audit apparmor  STATUS operationprofileload profileunconfined namenvidiamodprobe commapparmorparser
kernel audit apparmor  STATUS operationprofileload profile  unconfined 
kernel audit apparmor  STATUS operation profileload
  • Related