Splunk Search

How can I run the extract command on field extracted by the rex command

seomaniv
Explorer

Cog in a larger machine, I have asked my Splunk team to improve the parsing on some of our logs, but it hasn't happened yet, so I need a way to parse complex events in my search.

I have run my raw event(s) through rex and gotten several fields of interest (eg. field1, field2, field3, field4). Field 3 is a json, which I can then run through spath to get the key:value pairs, but field4 is a different series of key:value pairs (formatted as field-name=[fieldValues]).

My question is, I run my rex, then I pipe, then I run spath, then... how do I run extract?

Example event:

2019-03-20 19:25:16.010 Container: 32 Fred {json}, {Date=[some date and time and zone], Another-field=[values=may have; subvalues=with spaces; but no internal closing bracket], and-so-on=[until the last field]}

Note: I am trying to keep it all simple other than field4, which is the basis of my question; I am not having a problem with rex, so don't focus on my rex expressions (I won't even put them), but I can easily extract the other fields with rex, as well as the json fields with the spath:

rex field=_raw "Container: (?<field1>AppropriateRex) (?<field2>AppropriateRex) (?<field3>AppropriateRex),\{(?<field4>AppropriateRex)\}" | spath input=field3

so I would get:

field1=32
field2=Fred
field3={json}
field4

Date=[some date and time and zone], Another-field=[values=may have; subvalues=with spaces; but no internal closing bracket], and-so-on=[until the last field]

and let's say all 50 of the fields from my json are properly extracted via spath. How do I add an "extract" command to get the key:value pairs from field4? I'd like to:

| extract input=field4 ...

but extract doesn't have an "input" parameter.

Thank's in advance for your help.

0 Karma
1 Solution

sduff_splunk
Splunk Employee
Splunk Employee

You can try renaming the field you want to extract from to _raw , then run extract

rex field=_raw "Container: (?<field1>AppropriateRex) (?<field2>AppropriateRex) (?<field3>AppropriateRex),\{(?<field4>AppropriateRex)\}" | spath input=field3 | rename field4 AS _raw | extract

View solution in original post

sduff_splunk
Splunk Employee
Splunk Employee

You can try renaming the field you want to extract from to _raw , then run extract

rex field=_raw "Container: (?<field1>AppropriateRex) (?<field2>AppropriateRex) (?<field3>AppropriateRex),\{(?<field4>AppropriateRex)\}" | spath input=field3 | rename field4 AS _raw | extract

seomaniv
Explorer

Thank you. This allows the proper extraction of the new fields, as well as keeping the previously extracted fields available, but the event itself is reduced to field4 (I assume because we renamed it as _raw). Is there a way to extract the fields and still display the entire event? For my current needs, I am making a table out of the data to display to non-tech users, so the raw data is not terribly important, but I fear at some point in the future I will regret saying that ...

0 Karma

sduff_splunk
Splunk Employee
Splunk Employee

rex field=_raw "Container: (?AppropriateRex) (?AppropriateRex) (?AppropriateRex),\{(?AppropriateRex)\}" | spath input=field3 |eval orig_raw = _raw | rename field4 AS _raw | extract | eval _raw = orig_raw

0 Karma

seomaniv
Explorer

Thank you, sduff! That worked perfectly!

0 Karma
Get Updates on the Splunk Community!

Enterprise Security Content Update (ESCU) | New Releases

In December, the Splunk Threat Research Team had 1 release of new security content via the Enterprise Security ...

Why am I not seeing the finding in Splunk Enterprise Security Analyst Queue?

(This is the first of a series of 2 blogs). Splunk Enterprise Security is a fantastic tool that offers robust ...

Index This | What are the 12 Days of Splunk-mas?

December 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...