Splunk Search

Regex for field extraction

splunkn
Communicator

I am having a source file with the two below mentioned format. However I need to extract a same field but whose positions differ.
Could anyone help me with appropriate regex?

Jan 21 19:38:53 hostname sudo: pam_unix(sudo:session): session opened for user abc by xyz(uid=0)

Jan 21 19:38:38 hostname sshd[000]: Accepted password for xyz from port 123

Here I need to extract the user "xyz" . Both events belongs to same source. How could write the regex to match the both?

OR do we need to go with Field alias?

Thanks in advance

Tags (2)
0 Karma
1 Solution

pradeepkumarg
Influencer

One way is to extract both differently and then use coalesce function to grab the one that is not NULL

... |  rex "(?i)session opened for user \w+ by (?P<USER1>.\w+)\W"  |  rex "(?i)Accepted password for(?P<USER2>.\w+) from" 
    | eval USER = coalesce(USER1,USER2)

View solution in original post

0 Karma

pradeepkumarg
Influencer

One way is to extract both differently and then use coalesce function to grab the one that is not NULL

... |  rex "(?i)session opened for user \w+ by (?P<USER1>.\w+)\W"  |  rex "(?i)Accepted password for(?P<USER2>.\w+) from" 
    | eval USER = coalesce(USER1,USER2)
0 Karma
Get Updates on the Splunk Community!

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

Watch On Demand the Tech Talk on November 6 at 11AM PT, and empower your SOC to reach new heights! Duration: ...

Splunk Observability as Code: From Zero to Dashboard

For the details on what Self-Service Observability and Observability as Code is, we have some awesome content ...