Splunk Search

How do I convert duration to minutes

gill1723
Engager

index=main host=10.247.82.1 user=* | rex field=duration "((?\d+)h:)?(?\d+)m:(?\d+)s" | eval duration=duration_second+60*duration_minute+3600*coalesce(duration_hour,0) | stats sum(duration) as total_duration by Username, date_month, date_mday | bin _time span=1d

Results:
date month total duration
april 24 2657

Tags (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Try this.

index=main host=10.247.82.1 user=* | rex field=duration "((?<duration_hour>\d+)h:)?(?<duration_minute>\d+)m:(?<duration_second>\d+)s" | eval duration=duration_second+60*duration_minute+3600*coalesce(duration_hour,0) | stats sum(duration) as total_duration by Username, date_month, date_mday | bin _time span=1d | eval total_duration=tostring(total_duration, "duration")
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

Try this.

index=main host=10.247.82.1 user=* | rex field=duration "((?<duration_hour>\d+)h:)?(?<duration_minute>\d+)m:(?<duration_second>\d+)s" | eval duration=duration_second+60*duration_minute+3600*coalesce(duration_hour,0) | stats sum(duration) as total_duration by Username, date_month, date_mday | bin _time span=1d | eval total_duration=tostring(total_duration, "duration")
---
If this reply helps you, Karma would be appreciated.

rbansql104
New Member

how can I declare a metrics in which I have the data like 30 days 10 hours 16 minutes have to be converted into seconds?

0 Karma

gill1723
Engager

I get this error when I run that script

⚠ Error in 'rex' command: Encountered the following error while compiling the regex '((?\d+)h:)?(?\d+)m:(?\d+)s': Regex: unrecognized character after (? or (?-

0 Karma

richgalloway
SplunkTrust
SplunkTrust

That's because I copied the search after the forum munged it. I've corrected my answer.

---
If this reply helps you, Karma would be appreciated.
0 Karma

gill1723
Engager

This works great thanks a bunch.

0 Karma

gill1723
Engager

I need to convert this to HH:MM:SS

index=main host=10.247.82.1 user=* | rex field=duration "((?\d+)h:)?(?\d+)m:(?\d+)s" | eval duration=duration_second+60*duration_minute+3600*coalesce(duration_hour,0) | stats sum(duration) as total_duration by Username, date_month, date_mday | bin _time span=1d

0 Karma

p_gurav
Champion

Can you try something:

| convert dur2sec(duration) as duration_sec |eval min=duration_sec/60

gill1723
Engager

Sorry I need to convert it to HH:MM:SS

index=main host=10.247.82.1 user=* | rex field=duration "((?\d+)h:)?(?\d+)m:(?\d+)s" | eval duration=duration_second+60*duration_minute+3600*coalesce(duration_hour,0) | stats sum(duration) as total_duration by Username, date_month, date_mday | bin _time span=1d

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...