Splunk Cloud Platform

How to extract N digits based on field length?

WesleyFranklin
Explorer

Hi everyone,
I have the following search:

index=xyz
| eval new_field = field
| eval length=len(new_fied)
| eval new_field = substr(new_field, 1, len(new_field) -15)
| table new_field,field,length

With that I'm removing the last 15 digits of "new_field" 

So this query works but the problem is that my "new_field" have 4 different lenghts and for each one I need to do something different then I'd like to create a condition to extract last 15 numbers only if the lenght is 27 (one of the lenghts)

sample:
011332255667799114466990033 turns out > 011332255667

I was trying to do something like below:

| eval new_field = if((len(new_field)==27, "0", new_field)substr(new_field, 1, len(new_field) -15))

For sure this is not working, but just to show my idea and what I need to accomplish. When the "new_field" has <> 27 digits it goes wrong

Any idea?

Thanks

Tags (1)
0 Karma
1 Solution

gcusello
Legend

Hi @WesleyFranklin,

you eval command has two problems:

with the first eval you take you number without the last 15 digits and not the last 15 digits, then the second eval is wrong.

So if you wabt  to extract last 15 numbers only if the lenght is 27, you should try something like this:

| eval new_field=if(len(new_field)=27,substr(new_field,len(new_field)-15,15),new_field)

Ciao.

Giuseppe

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

Depending on how much you want to remove for each of your 4 lengths, you could do something like this

| eval length=len(new_field)
| eval new_field = case(length=27,substr(new_field, 1, length -15),length=30,substr(new_field,1,length-20),1==1,new_field)

WesleyFranklin
Explorer

Hi @ITWhisperer 

 

Thank you for your reply.

It worked.

Is it possible also remove the fisrt 5 numbers THEN remove the last 15 using case?

0 Karma

gcusello
Legend

Hi @WesleyFranklin,

you eval command has two problems:

with the first eval you take you number without the last 15 digits and not the last 15 digits, then the second eval is wrong.

So if you wabt  to extract last 15 numbers only if the lenght is 27, you should try something like this:

| eval new_field=if(len(new_field)=27,substr(new_field,len(new_field)-15,15),new_field)

Ciao.

Giuseppe

WesleyFranklin
Explorer

Hi @gcusello 

Thank you for your reply.

It worked.

Is it possible also remove the fisrt 5 numbers THEN remove the last 15 using this if condition?

0 Karma

gcusello
Legend

Hi @WesleyFranklin,

yes, it's possible.

Only to understand: do you want to remove the first 5 digits of the field when lenght is greather then 27 and after remove always the last 15 digitis, or do you want to take the last 10 digits ?

In the second case the solution is the same of the previous answer, in the second case you have to use twise the eval command:

 

| eval new_field=if(len(new_field)>=27,substr(new-field),6, 100)
| eval new_field=if(len(new_field)>=22,substr(new_field,len(new_field)-15,15),new_field)

 

I use the first eval to delete the first 5 digits and the second eval to tak the following 15 digits

Anyway, see the approach and modify the eval commands to adapt them to your need:

Ciao.

Giuseppe

P.S.: Karma Points are appreciated by all the Contributors 😉

 

WesleyFranklin
Explorer

Hey @gcusello 

Oh I see, now I'll adjust as far as I need it.

That's awesome.


Thank you so much.

0 Karma

gcusello
Legend

Hi @WesleyFranklin,

good for you, see next time!

please accept one answer for the other people of Community.

Ciao and happy splunking

Giuseppe

P.S.: Karma Points are appreciated by all the Contributors 😉

0 Karma
Get Updates on the Splunk Community!

Introducing Ingest Actions: Filter, Mask, Route, Repeat

WATCH NOW Ingest Actions (IA) is the best new way to easily filter, mask and route your data in Splunk® ...

Splunk Forwarders and Forced Time Based Load Balancing

Splunk customers use universal forwarders to collect and send data to Splunk. A universal forwarder can send ...

NEW! Log Views in Splunk Observability Dashboards Gives Context From a Single Page

Today, Splunk Observability releases log views, a new feature for users to add their logs data from Splunk Log ...