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!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...