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!

Accelerating Observability as Code with the Splunk AI Assistant

We’ve seen in previous posts what Observability as Code (OaC) is and how it’s now essential for managing ...

Integrating Splunk Search API and Quarto to Create Reproducible Investigation ...

 Splunk is More Than Just the Web Console For Digital Forensics and Incident Response (DFIR) practitioners, ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...