Splunk Search

How to write a rex extraction based on the count of a previous field value?

dkarthik16
New Member

I have a log like this

1000107KARTHIk100203YES

I want to extract like this
1000 07 KARTHIK
1002 03 RITHVIK

where
1000 - Fixed length
07 - Fixed length
KARTHIK - Based on the previous value (07) I need to set the length of the this field.

I tried like this

"(?P<FieldId>[^}]{3}+)(?P<FieldCount>[0-9]{2}+)(?P<FieldValue>[0-9a-zA-Z]{**?**}+)"

Thanks in advance!

0 Karma
1 Solution

woodcock
Esteemed Legend

I can't think of a way to do it in a single RegEx but how about 2 pipelined commands like this:

... | rex "(?<FieldId>.{4})(?<FieldCount>.{2})(?<FieldValue>.*)" | eval FieldValue=substr(FieldValue,1,FieldCount)

View solution in original post

0 Karma

woodcock
Esteemed Legend

I can't think of a way to do it in a single RegEx but how about 2 pipelined commands like this:

... | rex "(?<FieldId>.{4})(?<FieldCount>.{2})(?<FieldValue>.*)" | eval FieldValue=substr(FieldValue,1,FieldCount)
0 Karma

dkarthik16
New Member

First of all thanks for the reply...
Here I cannot able to use max_match with rex by which I can able to find out all the records in that line.

0 Karma

dkarthik16
New Member

Hi WoodCock,

I can able to extract the information from the log using your answer which you posted with one of my colleague's question (the same question) with minor modification of query.

| rex field=_raw "(?.{3})(?.{2})(?.*)"
| eval FieldValue=substr(TempFieldValue,1,FieldValueLen)
| eval TempFieldValue=substr(TempFieldValue,1+FieldValueLen)
| eval subevent=(TransactionCode . ":::" . FieldValueLen . ":::" . FieldValue)

| rex field=TempFieldValue "(?.{3})(?.{2})(?.*)"
| eval TransactionCode=mvappend(TransactionCode, TempTransactionCode)
| eval FieldValueLen=mvappend(FieldValueLen, TempFieldValueLen)
| eval FieldValue2=substr(TempFieldValue,1,TempFieldValueLen)
| eval FieldValue=mvappend(FieldValue, FieldValue2)
| eval TempFieldValue=substr(TempFieldValue,1+TempFieldValueLen)
| eval subevent=mvappend(subevent, TempTransactionCode . ":::" . TempFieldValueLen . ":::" . FieldValue2)

| mvexpand subevent
| rex max_match=5 field=subevent "(?.?):::(?.?):::(?.*)"

| table TransactionCode FieldValueLen FieldValue

By repeating the Bold set of query i can able to extract all the information from the log.

Is there any way to auto iterate this query (loop)? So that i can able to extract "n" number of such records upto end of line which is more or less equal to max_match.

0 Karma

woodcock
Esteemed Legend

Because we could not get the first pass into a single rex command, the answer is "no". However, we can clean it up quite a bit by putting part or all of it into a macro, which is what I suggest that you do.

0 Karma

dkarthik16
New Member

Sure will try that..Thanks

0 Karma

woodcock
Esteemed Legend

Don't forget to "Accept" an answer to close of the question (up-voting useful comments is also appreciated).

0 Karma

dkarthik16
New Member

Sorry for the mistake in the sample data
1000 07 KARTHIK
1002 07 RITHVIK

0 Karma
Get Updates on the Splunk Community!

Database Performance Sidebar Panel Now on APM Database Query Performance & Service ...

We’ve streamlined the troubleshooting experience for database-related service issues by adding a database ...

IM Landing Page Filter - Now Available

We’ve added the capability for you to filter across the summary details on the main Infrastructure Monitoring ...

Dynamic Links from Alerts to IM Navigators - New in Observability Cloud

Splunk continues to improve the troubleshooting experience in Observability Cloud with this latest enhancement ...