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
SplunkTrust
SplunkTrust

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
SplunkTrust
SplunkTrust

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
SplunkTrust
SplunkTrust

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
SplunkTrust
SplunkTrust

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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

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

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...