Splunk Search

Help with regex for field extraction

mbasharat
Builder

Hi,

I have a field value as below.
These are all fixed positions all across.

/COMPANY LOCATIONS/PA/PHILADELPHIA/ABC - PHILADELPHIA/

I need two regex:

  1. Only State extracted from above new fields labeled as STATE
  2. All fields extracted as LOCATION, STATE, CITY, CODE

Thanks in advance!!!

0 Karma
1 Solution

mbasharat
Builder

After banging my head for a bit, I coded below run any where and it worked!!! 🙂

| gentimes start=-1 

| eval Location="/COMPANY LOCATIONS/PA/PHILADELPHIA/ABC - PHILADELPHIA/"

| table Location

| rex field=Location "^(?:[^\/\n]*\/){2}(?P<State>\w+)\/(?P<City>[^\/]+)\/(?P<Code>[^\/]+)"

View solution in original post

0 Karma

woodcock
Esteemed Legend

Like this:

| makeresults 
| eval _raw="/COMPANY LOCATIONS/PA/PHILADELPHIA/ABC - PHILADELPHIA/" 
| rex "^\/(?<WHATEVER>[^\/]+)\/(?<LOCATION>[^\/]+)\/(?<STATE>[^\/]+)\/(?<CITY>[^\/]+)\/"

I am not sure what is supposed to be CODE ... ?

0 Karma

mbasharat
Builder

After banging my head for a bit, I coded below run any where and it worked!!! 🙂

| gentimes start=-1 

| eval Location="/COMPANY LOCATIONS/PA/PHILADELPHIA/ABC - PHILADELPHIA/"

| table Location

| rex field=Location "^(?:[^\/\n]*\/){2}(?P<State>\w+)\/(?P<City>[^\/]+)\/(?P<Code>[^\/]+)"
0 Karma

arjunpkishore5
Motivator

Could you please provide some more sample inputs and the expected output. For example, when you say you need LOCATION, STATE, CITY, CODE, what values should each of these hold with respect to your sample data?

0 Karma

mbasharat
Builder

Hi Arjun. See my own answer to my own question. Took some head bangs at this time of Friday! 🙂

0 Karma
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...