Splunk Search

How to extract keywords and associated values with rex from raw logs and display data in a table?

satya2p
Path Finder

I have a request input output logged by various sourcetypes in XML and other similar below format. I tried multiple options to extract exact keywords and associated values to display in a table, but I have been unable to do so. Please help.

Sample Data:

Somecontent.ssn=XXXXXXXXX.Somecontent.SomeOthercontent
in XML: <SSN> XXXXXXXXX </SSN>

Options Tried:

| rex field=_raw "SSN=(?P\d\d\d-?\d\d-?\d\d\d\d)" | stats count by SSN
| rex field=_raw=\W+SSN "(?\w+)" | stats count by SSN
| rex field=_raw "SSN\=(?\d{9})+"| stats count by SSN
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Your rex commands are not extracting fields. Therefore, there is nothing for downstream commands to work with. Try this:

... | rex "SSN\>\s*(?P<SSN>\d{9})" | stats count by SSN

The first "SSN" is an eyecatcher to help rex find the right data in your XML. The second "SSN" is a field name which can be used by the stats command.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Your rex commands are not extracting fields. Therefore, there is nothing for downstream commands to work with. Try this:

... | rex "SSN\>\s*(?P<SSN>\d{9})" | stats count by SSN

The first "SSN" is an eyecatcher to help rex find the right data in your XML. The second "SSN" is a field name which can be used by the stats command.

---
If this reply helps you, Karma would be appreciated.
0 Karma

satya2p
Path Finder

Thank you.. I tried, its just reading all SSN keyword(considering false positive) not extracting the values and listing out.

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Let's try something simpler. This should display all the SSN values found.

... | rex "SSN\>\s*(?P<SSN>\d{9})" | table SSN
---
If this reply helps you, Karma would be appreciated.
0 Karma

satya2p
Path Finder

I tried this option earlier, table getting generated for each event logged by keyword but data is not populating. seems its unable to extract values. I am using splunk 5.0.9, is xml filed extraction is available in this release.

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Yes, field extraction using the rex command is the same in version 5. Is the sample data in your OP accurate? Regex strings can be very sensitive to differences in white space, case, etc.

---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Dynamic formatting from XML events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  &#x1f680; Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...