Splunk Search

Need help normalizing a field's contents for display

DaClyde
Contributor

I'm extracting a piece of a filename to create a field using makemv and a rex command. The extracted field should be formatted like 89-02687, but sometimes occurs as 8902687. I want all of my output to show the proper formatting, so all the results have the XX-XXXXX format.

Could I use a tostring statement and a regex or a replace command to somehow insert the hyphen into any results that don't have it after the second digit?

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

You should be able to do it with rex.

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

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

You should be able to do it with rex.

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

DaClyde
Contributor

Here's where I put the line:

| rex field=filename "(?:[^.\n]*.){2}(?P<RDFTAIL>[^.]+)" 
| stats sum(filesize) as Bytes by cbmfolder,RDFTAIL,Date
| eval MB = Bytes/1024/1024 
| eval MB=round(MB,1) 
| rex mode=sed field=RDFTAIL "s/(\d{2})(\d{5})/\1-\2"

But I get this error:

⚠ Error in 'rex' command: Failed to initialize sed. Failed to parse the replacement string.

0 Karma

richgalloway
SplunkTrust
SplunkTrust

I forgot to close the sed string. See the revised answer.

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

DaClyde
Contributor

Ah, beautiful, works perfectly. Thanks!

0 Karma
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...