Splunk Search

REX / REGEX question

dbrown0412
Engager

Hi all, I am new to splunk. I am trying to extract a field from a line in a record where the field will always begin with a 2 letter state code followed by 8 additional charaters. The record also contains a two letter state field I would like to use as a variable for the extraction search, since the state will vary from record to record. Also the position of this field to be extracted will vary from record to record. I have played with multiple variations of REX and REGEX with EVAL and havent found the answer for reading a variable into the expression, or allowing the extraction position to vary across different records.

Any help would be greatly appreciated.

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

If the location of the state varies within the comments field and the format is consistent with your example, then this should find it.

\s+(?<state>[A-Z]{2}[0-9]{8})\s+
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

If the location of the state varies within the comments field and the format is consistent with your example, then this should find it.

\s+(?<state>[A-Z]{2}[0-9]{8})\s+
---
If this reply helps you, Karma would be appreciated.

richgalloway
SplunkTrust
SplunkTrust

Please accept the answer to help future readers.

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

dbrown0412
Engager

Thanks all. Changed the field to _raw and it works great

0 Karma

theouhuios
Motivator

Sorry for not seeing your answer when I updated mine 🙂

0 Karma

dbrown0412
Engager

Yes, the location of CA12345678 will vary within the comments field

0 Karma

theouhuios
Motivator

Try this

   (?P<State>[A-Z]{2}[0-9]{8})\s+
0 Karma

theouhuios
Motivator

Will the location of CA12345678 also be changing? If not try this "\,\w+\s+\w+\s+(?P[A-Za-z0-9]+)\s+"

0 Karma

dbrown0412
Engager

Here is an example of what a comments field looks like. The CA12345678 is what I need to extract. The state can vary, as can the location of the data needed with the comments.

FT JOE BB72649 BBB 9998372615 FT REQ ASST W/ACT ANYTHING VALID ABC,CURRENT WORDS CA12345678 NOT ALLOWING NEW BOGUS PHRASES TO GO THROUGH.ADVSD FT.FOO USELESS

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Please provide some sample events (data). At least the comments field.

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

dbrown0412
Engager

Hi. Sure. Here is the most recent example I have been working with.

sourcetype=file earliest=-1d@d latest=now |eval temp=STATE | rex field=comments "(?$temp$\S{8})"| stats count as total by field1, field2,field3 | table field1,field2,field3 |sort -total|head 10

If I remove $temp$ and use [NY] for example, I get some valid responses, but it doesn't seem to be handling the varying start position of the data in the record.

0 Karma

MuS
SplunkTrust
SplunkTrust

could you please provide some samples?

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Meet Splunk Observability Studio: AI-Assisted OpenTelemetry Instrumentation Without ...

Instrumentation is usually the last step or even an afterthought when building out a project. The feature ...

Federated Search for Cisco Security and Analytics Logging (SAL) is now GA on Splunk ...

Federated Search for Cisco  Security Analytics and Logging (SAL) is now generally available as part of the ...

Your Path to AgenticOps: AI Experiences for Every Splunk Practitioner

Your Path to AgenticOps: AI Experiences for Every Splunk Practitioner   Join us for a demo-driven look at how ...