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
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...