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!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...