Splunk Search

How to use rex and sed to insert '-' and ':' in the result?

nilotpaldutta
Explorer

Hi, I'm new to Splunk. I have a query that extracts the date and time from the name of a log file. Logfile names are like e.g. XXXXXXXX_20150615133030.log. My query successfully returns the desired output which is 20150615133030. This is as per my requirement.

Now, i would like to edit the number to show like this -- "2015-06-15 13:30:30".
I tried the following command in bash prompt and it works -- sed 's/^\(.\{4\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1-\2-\3 \4:\5:/g' numbers.txt and it works fine. But this is not working when i use it in my Splunk query.

Please answer if anyone knows. Thanks in advance.

Tags (2)
1 Solution

yannK
Splunk Employee
Splunk Employee

in the search query, the sed string is between double quotes. Therefore you have to escape or double escape some symbols.

PS: in the props.conf you do not need the extra escape.

View solution in original post

yannK
Splunk Employee
Splunk Employee

in the search query, the sed string is between double quotes. Therefore you have to escape or double escape some symbols.

PS: in the props.conf you do not need the extra escape.

stephanefotso
Motivator

Also, why don't you edit your props.conf for it? I think it will be easy!

SGF
0 Karma

nilotpaldutta
Explorer

Thanks for your response. Can you please post an example?
I'm not looking to standardize my output. Just need it once for the above query.
I might be wrong but isn't editing any config file going to always return results of other queries also in one particular format?

0 Karma

richgalloway
SplunkTrust
SplunkTrust

What is the Splunk query that is failing?

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

nilotpaldutta
Explorer

index=myindex | dedup source | sort -source | dedup sourcetype | rex field=source mode=sed "s/[^0-9]*//g" | rename source as date | rex field=date mode=sed "s/(\d{4}-){1}/2015-/g" | table sourcetype, date

source and sourcetype are two fields i'm retrieving.

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Your search is failing because the date field does not have a hyphen in it. This should work (your original sed string has far too many escapes).

rex field=date mode=sed "s/(.{4})(.{2})(.{2})(.{2})(.{2})/\1-\2-\3 \4:\5:/"
---
If this reply helps you, Karma would be appreciated.

nilotpaldutta
Explorer

Thank you. This works for me. 🙂

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Please accept the answer.

---
If this reply helps you, Karma would be appreciated.
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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

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

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...