Splunk Search

Need string minus last 2 characters

rachelneal
Path Finder

I am trying to set a field to the value of a string without the last 2 digits. For example:

Hotel=297654 from 29765423
Hotel=36345 from 3624502

I tried rtrim but docs say you must know the exact string you're removing, mine are different every time. I also tried substr but the length is not constant from 1st character and using -2 like substr(Field, -2) returns the last 2 characters...the part I want to throw away.

Was trying something like rtrim(Field,substr(Field,-2) but can't get that to work.

Suggestions? - THANKS

Tags (1)
0 Karma
1 Solution

_d_
Splunk Employee
Splunk Employee

Or another way:

<your_search_here> | eval myfield = substr(myfield, 1, len(myfield)-2)

d.

-
please upvote if you find this answer useful

View solution in original post

_d_
Splunk Employee
Splunk Employee

Or another way:

<your_search_here> | eval myfield = substr(myfield, 1, len(myfield)-2)

d.

-
please upvote if you find this answer useful

trijit
Engager

Can we do it the reverse way like:

string minus first 2 characters ?

0 Karma

_d_
Splunk Employee
Splunk Employee

Hi Rachel,
Using substr(myfield, 1, len(myfield)-2) is your best bet here. It turns out that rtrim will remove ALL matching characters that you supply:
ex. if a=201145454545 and you do b=rtrim(a, "45") b will end being: 2011. Also, sometimes Splunk will use scientific notation after a certain number length.

0 Karma

_d_
Splunk Employee
Splunk Employee

Yup, just tested it and you're right. It appears that this is happening with numbers only. Probably it has to do with arithmetic routines. I'll do some further investigation and perhaps touch base with engineering. Thanks.

0 Karma

rachelneal
Path Finder

Thanks d

Out of curiosity, do you know why "eval Hotel=rtrim(Hotel,substr(Hotel,-2)) " where Hotel was 29272905 returned 2.92729e04?

0 Karma

_d_
Splunk Employee
Splunk Employee

Hi RachelNeal,

You can extract another field and do something like this:

<your_search_here> | rex field=myfield "(?<myfield>.*).{2}"

This would drop off the last two chars of myfield.

Get Updates on the Splunk Community!

Fueling your curiosity with new Splunk ILT and eLearning courses

At Splunk Education, we’re driven by curiosity—both ours and yours! That’s why we’re committed to delivering ...

Splunk AI Assistant for SPL 1.1.0 | Now Personalized to Your Environment for Greater ...

Splunk AI Assistant for SPL has transformed how users interact with Splunk, making it easier than ever to ...

Unleash Unified Security and Observability with Splunk Cloud Platform

     Now Available on Microsoft AzureOn Demand Now Step boldly into the AI revolution with enhanced security ...