Splunk Search

How to extract a date from a string?

rmuraly
Explorer

Hi,

I have a string
'ABC_GFD_NOCS_RPT_HIST_2017-05-12_5min.csv'

How do I extract '2017-05-12' from 'ABC_GFD_NOCS_RPT_HIST_2017-05-12_5min.csv' in my saved search?

0 Karma
1 Solution

niketn
Legend

You might have to add mocked up raw data and also your search for us to help you better. However, based on what you have provided please try following regular expression:

| rex field=_raw "_(?<Date>\d{4}-\d{2}-\d{2})_"
| table Date _raw
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

View solution in original post

niketn
Legend

You might have to add mocked up raw data and also your search for us to help you better. However, based on what you have provided please try following regular expression:

| rex field=_raw "_(?<Date>\d{4}-\d{2}-\d{2})_"
| table Date _raw
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

niketn
Legend

@rmuraly, I have converted my comment as answer. Please accept to mark the question as answered.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

rmuraly
Explorer

I had to extract the date from my source file and this helps me do it.

| stats count | eval source="ABC_GFD_NOCS_RPT_HIST_2017-05-12_5min.csv"| rex field=source"(?\d{4}-\d{2}-\d{2})"
| table Date,source

Thank you

0 Karma

somesoni2
Revered Legend

If this string is part of an already extracted field, say file_path, then in rex command, use file_path instead of _raw.

0 Karma

rmuraly
Explorer

I had to extract the date from my source file and this helps me do it.

| stats count | eval source="ABC_GFD_NOCS_RPT_HIST_2017-05-12_5min.csv"| rex field=source"(?\d{4}-\d{2}-\d{2})"
| table Date,source

Thank you

0 Karma
Get Updates on the Splunk Community!

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...