Splunk Search

How to convert string time to seconds

sashib
Explorer

I have a TimeField with data format is like  4 Days 14 Hours 40 Minutes  and sometimes 7 Hours 40 Minutes

TimeField
4 Days 14 Hours 40 Minutes
7 Hours 40 Minutes
40 Minutes

 

I want to convert this field values into seconds so that i can sort my data based on time.

Thanks!

Labels (2)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@sashib 

Can you please try this?

YOOUR_SEARCH
| rex field=TimeField "(((?<Days>\d.*)\sDays\s)?(?<Hours>\d.*)\sHours\s)?(?<Minutes>\d.*)\sMinutes"
| fillnull value=0
| eval secs=(Days*86400)+(Hours*3600)+(Minutes*60)

 

My Sample Search :

| makeresults
| eval _raw="TimeField
4 Days 14 Hours 40 Minutes
7 Hours 40 Minutes
40 Minutes"
| multikv forceheader=1
| rex field=TimeField "(((?<Days>\d.*)\sDays\s)?(?<Hours>\d.*)\sHours\s)?(?<Minutes>\d.*)\sMinutes"
| fillnull value=0
| eval secs=(Days*86400)+(Hours*3600)+(Minutes*60)

 

Screenshot 2021-07-13 at 12.44.30 PM.png

Thanks
KV
▄︻̷̿┻̿═━一

If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated. 


 

View solution in original post

kamlesh_vaghela
SplunkTrust
SplunkTrust

@sashib 

Can you please try this?

YOOUR_SEARCH
| rex field=TimeField "(((?<Days>\d.*)\sDays\s)?(?<Hours>\d.*)\sHours\s)?(?<Minutes>\d.*)\sMinutes"
| fillnull value=0
| eval secs=(Days*86400)+(Hours*3600)+(Minutes*60)

 

My Sample Search :

| makeresults
| eval _raw="TimeField
4 Days 14 Hours 40 Minutes
7 Hours 40 Minutes
40 Minutes"
| multikv forceheader=1
| rex field=TimeField "(((?<Days>\d.*)\sDays\s)?(?<Hours>\d.*)\sHours\s)?(?<Minutes>\d.*)\sMinutes"
| fillnull value=0
| eval secs=(Days*86400)+(Hours*3600)+(Minutes*60)

 

Screenshot 2021-07-13 at 12.44.30 PM.png

Thanks
KV
▄︻̷̿┻̿═━一

If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated. 


 

sashib
Explorer

@bowesmana 

I tried your suggestion, 

TimeFielddayshoursminutessecs
4 Days 14 Hours 40 Minutes400345600
6 Hours 20 Minutes06021600

 

for the first entry giving only days

and for second entry its giving only hours

Thank you!

0 Karma

bowesmana
SplunkTrust
SplunkTrust

This search sets up your data example then calculates the seconds.

See from the rex statement onwards

| makeresults
| eval _raw="TimeField
4 Days 14 Hours 40 Minutes
7 Hours 40 Minutes
40 Minutes"
| multikv forceheader=1
| table TimeField
| mvexpand TimeField
| rex field=TimeField "((?<days>\d+) Days)?\s*?((?<hours>\d+) Hours)?\s*?((?<minutes>\d+) Minutes)?"
| fillnull value=0
| eval secs=(days*86400)+(hours*3600)+(minutes*60)
Get Updates on the Splunk Community!

Index This | Why did the turkey cross the road?

November 2025 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  &#x1f680; Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Feel the Splunk Love: Real Stories from Real Customers

Hello Splunk Community,    What’s the best part of hearing how our customers use Splunk? Easy: the positive ...