Splunk Search

How to convert the output of tostring or convert a duration field?

jameswatts
Explorer

I have a search that returns the diff of two times, but the user wants it in "1 day 5 hours and 23 minutes" format not 00+00:00:00. I know regex can do this, but the possibility that the day value might not be present has me stumped. Any help appreciated.

0 Karma
1 Solution

somesoni2
SplunkTrust
SplunkTrust

Try something like this (run anywhere example)

| gentimes start=-1 | eval diff=round(now()-relative_time(now(),"-2h")) | table diff | eval duration=tostring(diff,"duration") | eval duration2=replace(duration,"(\d*)\+*(\d+):(\d+):(\d+)","\1 days \2 hours \3 minutes \4 secs")

View solution in original post

somesoni2
SplunkTrust
SplunkTrust

Try something like this (run anywhere example)

| gentimes start=-1 | eval diff=round(now()-relative_time(now(),"-2h")) | table diff | eval duration=tostring(diff,"duration") | eval duration2=replace(duration,"(\d*)\+*(\d+):(\d+):(\d+)","\1 days \2 hours \3 minutes \4 secs")

benhooper
Communicator

The following regex can be used to suppress any trailing substrings:

(\d*)\+*(\d+):(\d+):(\d+)(.*)
0 Karma

jameswatts
Explorer

Perfect! Thanks much.

0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

Splunk is officially part of Cisco

Revolutionizing how our customers build resilience across their entire digital footprint.   Splunk ...

Splunk APM & RUM | Planned Maintenance March 26 - March 28, 2024

There will be planned maintenance for Splunk APM and RUM between March 26, 2024 and March 28, 2024 as ...