Splunk Search

conditional converion

KarunK
Contributor

Hi All,

I have a field called "diskin" which can have two values in two measurements

1) K for kilobytes

2) M for megabytes

eg: diskin=9.9M, diskin=948K etc

How do i auto covert them to a single measurement say in bytes during search time ?

Thanks

Regards

KK

Tags (1)
1 Solution

sideview
SplunkTrust
SplunkTrust

You can use the if() function in eval, along with the substr() function to check the last character of the diskin field:

... | eval bytes=if(substr(diskin,-1)=="M",diskin/(1024*1024),diskin/1024)

If you have more than two cases, it's cleaner to use the case() function. Even with only 2 cases, it's a bit of a tossup. Here's the same functionality but with the case() function.

...| eval bytes=case(substr(diskin,-1)=="M",diskin/(1024*1024), substr(diskin,-1)=="K", ,diskin/1024)

http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/CommonEvalFunctions

UPDATE:

Sorry I forgot that those suffixes will prevent the field from being treated as a numeric field. Splunk will sometimes forgive a certain amount of strangeness and treat values as numeric anyway, but indeed it does not in this case and the division above fails.

But you can easily make yourself a numeric field by clipping off the suffix, and the overall search language then looks like:

with the eval command's if() function:

... | eval suffix=substr(diskin,-1) | eval value=substr(diskin,0,length(diskin)-1) | eval bytes=if(suffix=="M",value/(1024*1024),value/1024)

with the case() function instead:

... | eval suffix=substr(diskin,-1) | eval value=substr(diskin,0,length(diskin)-1) | eval bytes=case(suffix=="M",value/(1024*1024),suffix=="K",value/1024)

View solution in original post

sideview
SplunkTrust
SplunkTrust

You can use the if() function in eval, along with the substr() function to check the last character of the diskin field:

... | eval bytes=if(substr(diskin,-1)=="M",diskin/(1024*1024),diskin/1024)

If you have more than two cases, it's cleaner to use the case() function. Even with only 2 cases, it's a bit of a tossup. Here's the same functionality but with the case() function.

...| eval bytes=case(substr(diskin,-1)=="M",diskin/(1024*1024), substr(diskin,-1)=="K", ,diskin/1024)

http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/CommonEvalFunctions

UPDATE:

Sorry I forgot that those suffixes will prevent the field from being treated as a numeric field. Splunk will sometimes forgive a certain amount of strangeness and treat values as numeric anyway, but indeed it does not in this case and the division above fails.

But you can easily make yourself a numeric field by clipping off the suffix, and the overall search language then looks like:

with the eval command's if() function:

... | eval suffix=substr(diskin,-1) | eval value=substr(diskin,0,length(diskin)-1) | eval bytes=if(suffix=="M",value/(1024*1024),value/1024)

with the case() function instead:

... | eval suffix=substr(diskin,-1) | eval value=substr(diskin,0,length(diskin)-1) | eval bytes=case(suffix=="M",value/(1024*1024),suffix=="K",value/1024)

KarunK
Contributor

Thanks it worked....

Cheers

KK

0 Karma

sideview
SplunkTrust
SplunkTrust

Oh right. No problem. it's easy. I'll update my answer.

0 Karma

KarunK
Contributor

Ies i have tried this but "diskin/1024" wont work since, diskin is alphanumeric (eg:diskin=9.9M/1024)

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Continue Your Federation Journey: Join Session 3 of the Bootcamp Series

To help practitioners build a stronger foundation, we launched the Data Management & Federation ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Casting Call: Compete in Cyber Games

Lights, Camera, SecOps: Apply to Compete in Cyber Games     Think you have what it takes to beat the clock? ...