Splunk Search

Best way to correct tostring(,"duration") bug?

nick405060
Motivator

Hi there,

| eval session_length=tostring(end-start,"duration") is not padding zeros correctly (I believe just for seconds?): 02:08:4.275

What's the most elegant way to fix this?

The best I got so far is rex field=session_length "(?<hm>\d\d:\d\d:)(?<sn>\d\.\d\d\d)" | eval session_length=if(isnull(hm),session_length,hm."0".sn) which I'm pretty sure is breakable

0 Karma
1 Solution

nick405060
Motivator

rex field=session_length mode=sed "s/:(\d)(:|\.)/:0\1\2/g"

Give me karma

View solution in original post

0 Karma

woodcock
Esteemed Legend

Like this:

... | rex field=session_length mode=sed "s/(\d)(\.\d+)$/0\1\2/ s/:(\d)$/:0\1/"
0 Karma

woodcock
Esteemed Legend

The one from @nick405060 is better.

0 Karma

nick405060
Motivator

Maybe, I'm honestly not totally sure if it only effects seconds or not. If it does, then mine could be cleaned up because you don't need (:|.). If it only effects seconds and minutes, mine is good. If it effects hours mine needs to be modded.

0 Karma

woodcock
Esteemed Legend

If yours handles both 02:08:4.275 and 02:08:4, then yours is better, but if it doesn't, then mine is better (because it does both).

0 Karma

nick405060
Motivator

rex field=session_length mode=sed "s/:(\d)(:|\.)/:0\1\2/g"

Give me karma

0 Karma
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...