Splunk Cloud Platform

## How to extract N digits based on field length?

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

Labels (3)

• ### troubleshooting

Tags (1)
1 Solution
Legend

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

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)``````
Explorer

It worked.

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

Legend

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

Explorer

It worked.

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

Legend

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 😉

Explorer

Hey @gcusello

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

That's awesome.

Thank you so much.

Legend

good for you, see next time!

Ciao and happy splunking

Giuseppe

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

Get Updates on the Splunk Community!

#### 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 ...

#### Last Chance to Submit Your Paper For BSides Splunk - Deadline is August 12th!

Hello everyone! Don't wait to submit - The deadline is August 12th! We have truly missed the community so ...