Splunk Search

How to change string from csv file to date format for proper sorting in search?

jwolach2
New Member

I have data that was imported from a .csv file. One of the field in the .csv file is called "date". However, when Splunk imports that field the data is a string in the format 8/22/2014. I would like to sort the data by this "date" field but, Splunk does not sort it correctly because it sees the data as a string and sorts based on left to right.

How can I make it so that I can sort on the "date" data as if it was an actual date?

Tags (4)
0 Karma

aweitzman
Motivator

Somewhere in your search string, add the following clause:

convert timeformat="%m/%d/%Y" mktime(date) as numdate

That will create a new field numdate containing the epoch numerical representation of your date field. You can then sort on numdate.

Read more about convert here:

http://docs.splunk.com/Documentation/Splunk/6.1.3/SearchReference/Convert

0 Karma

somesoni2
Revered Legend

You can try this

your base search to get data from csv | eval date=strptime(date,"%m/%d/%Y") | sort 0 date
0 Karma

jwolach2
New Member

Got it, I think.

sourcetype=csv | eval date=strptime(date,"%m/%d/%Y") | eval reformatted_date=strftime(date,"%m/%d/%Y") | sort 0 date AND inv | table date reformatted_date inv customer item vendor description | where (date > strptime("05/31/2013","%m/%d/%Y") AND date < strptime("07/01/2013","%m/%d/%Y"))

Thanks

0 Karma

jwolach2
New Member

Great!!! Thank you very much! So here's what I now have as my search statement.

sourcetype=csv | eval date=strptime(date,"%m/%d/%Y") | eval reformatted_date=strftime(date,"%m/%d/%Y") | sort 0 date AND inv | table reformatted_date inv customer item vendor | where reformatted_date < "01/31/14"

Now, I would like to select a date range to be displayed. How can I do that with the epoch numerical data?

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...

Customer success is front and center at .conf25

Hi Splunkers, If you are not able to be at .conf25 in person, you can still learn about all the latest news ...