Splunk Search

Replace A pattern Splunk

baranova
New Member

Hello Guys ,

I have a field Month that have values like this

1-2013
10-2014
9-2014

i would like to get 0X-YYYY if month <10

so 1-2013 becomes 01-2013
10-2014 stays 10-2014
9-2014 becomes 09-2014

I've tried rex mode=sed field="Month" "s/(^[0-9]-.*)/0\1/" (it works fine on linux but not on Splunk ..)

Could you help me ?

Thanks !

Tags (1)
0 Karma

grijhwani
Motivator

I'm not familiar with sed mode, but have you tried escaping the square brackets with a backslash. Whenever a regex fails to match the expected results my first thought is always escaping special characters. Some regex modes assume special meaning unless expressly suppressed whilst others are the reverse, and just for fun some assume special meaning for one subset whilst not for others. It can be mind-bendingly confusing.

0 Karma

HiroshiSatoh
Champion

about using eval?

・・・|eval Month=if(len(Month)==6,"0"+Month,Month)

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!

What Is Splunk? Here’s What You Can Do with Splunk

Hey Splunk Community, we know you know Splunk. You likely leverage its unparalleled ability to ingest, index, ...

Level Up Your .conf25: Splunk Arcade Comes to Boston

With .conf25 right around the corner in Boston, there’s a lot to look forward to — inspiring keynotes, ...

Manual Instrumentation with Splunk Observability Cloud: How to Instrument Frontend ...

Although it might seem daunting, as we’ve seen in this series, manual instrumentation can be straightforward ...