Splunk Search

tonumber() not working

jedatt01
Builder

I have an extracted field that is alphanumeric and splunk is interpreting it as a string, obviously. But I am using rtrim to remove the alpha characters and leave only numeric characters. But as I confirmed with if(isstr()) splunk is still interpreting the field as a string. So, I tried to convert to number with tonumber() but results are coming back null. Any suggestions?

Tags (2)
1 Solution

Wilcooley
Path Finder

I don't suppose you could give an example of the field? That would help immensely. I'm guessing it's something like "100 kWH"?

Have you confirmed that eval xx=rtrim(Total_Energy,"kWH") results in something that looks like a number? Is there a space in your "kWH" that I can't see and is there a space between the number and the string? A trailing space might make the rtrim output look not like a number and would be hard to see.

rtrim seems kinda like an odd choice, though it seems like it should work. Personally I would have used a regular expression with replace:

replace(Total_Energy, "^(\d+)", "\1")

View solution in original post

Wilcooley
Path Finder

I don't suppose you could give an example of the field? That would help immensely. I'm guessing it's something like "100 kWH"?

Have you confirmed that eval xx=rtrim(Total_Energy,"kWH") results in something that looks like a number? Is there a space in your "kWH" that I can't see and is there a space between the number and the string? A trailing space might make the rtrim output look not like a number and would be hard to see.

rtrim seems kinda like an odd choice, though it seems like it should work. Personally I would have used a regular expression with replace:

replace(Total_Energy, "^(\d+)", "\1")

jedatt01
Builder

That was the problem. I was forgetting about the space before the kWH. this worked correctly
eval xx=rtrim(Total_Energy, " kWH")
Thanks!!

0 Karma

jedatt01
Builder

figured out a workaround from another post that converts my string to a number. Could there be a bug in tonumber()?

eval TE=strptime(rtrim(Total_Energy,"kWH"),"%s")

0 Karma
Get Updates on the Splunk Community!

Technical Workshop Series: Splunk Data Management and SPL2 | Register here!

Hey, Splunk Community! Ready to take your data management skills to the next level? Join us for a 3-part ...

Spotting Financial Fraud in the Haystack: A Guide to Behavioral Analytics with Splunk

In today's digital financial ecosystem, security teams face an unprecedented challenge. The sheer volume of ...

Solve Problems Faster with New, Smarter AI and Integrations in Splunk Observability

Solve Problems Faster with New, Smarter AI and Integrations in Splunk Observability As businesses scale ...