Splunk Search

Replace First Two Digits

IRHM73
Motivator

Hi,

I wonder whether someone may be able to help me please.

I have a telephone number field "telnofac" with the first two digits being 44.

Could someone tell me please is there a way to replace these the 44 with a 0?

Many thanks and kind regards

Chris

0 Karma
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi IRHM73,
two additional information:

  • do you want to replace numbers at index time or at search time?
  • do you want replace 44 with 0 or with 044?

if at index time and you want to replace 44 with 0, you have to insert in your props.conf in your sourcetype stanza (if before number there's "telnofac=")

SEDCMD-telnofac = s/telnofac\=44\d*/telnofac\=0\d*/g

(check regex!)

if you want to do this at search time and replace 44 with 0 use this command

| rex field=telnofac mode=sed "s/44\d*/0\d*/g"

or

| eval telnofac = replace(telnofac , "44\d*","0\d*")

Bye.
Giuseppe

View solution in original post

gcusello
SplunkTrust
SplunkTrust

Hi IRHM73,
two additional information:

  • do you want to replace numbers at index time or at search time?
  • do you want replace 44 with 0 or with 044?

if at index time and you want to replace 44 with 0, you have to insert in your props.conf in your sourcetype stanza (if before number there's "telnofac=")

SEDCMD-telnofac = s/telnofac\=44\d*/telnofac\=0\d*/g

(check regex!)

if you want to do this at search time and replace 44 with 0 use this command

| rex field=telnofac mode=sed "s/44\d*/0\d*/g"

or

| eval telnofac = replace(telnofac , "44\d*","0\d*")

Bye.
Giuseppe

IRHM73
Motivator

Hi @cusello, thank you for the comprehensive reply.

I want to make the change at search time, so I used the second solution you provided but unfortunately, it didn't work with the output of the field now showing:

0\d*

Many thanks and kind regards

Chris

0 Karma

IRHM73
Motivator

Hi @cusello and all.

I was able to make a working solution using: sed "s/44/0/g"

Many thanks and kind regards

Chris

0 Karma

s2_splunk
Splunk Employee
Splunk Employee

Chris, you are aware that this will change all occurrences of 44 with 0, so if your telnofac is 4412345446789, it will result in 01234506789; probably not what you want.

I would change it to | rex field=telnofac mode=sed "s/^44/0/" to only replace the first occurrence, anchored to the beginning of the field, just to be safe.

0 Karma

IRHM73
Motivator

Hi @ssievert, that's great I hadn't realise that.

Thank you for taking the time to reply.

May I ask what the ^ does.

Many thanks and kind regards

Chris

0 Karma

s2_splunk
Splunk Employee
Splunk Employee

"^" anchors to the beginning of the string. See here.

0 Karma

IRHM73
Motivator

Many thanks @ssievert.

Regards

Chris

0 Karma

gcusello
SplunkTrust
SplunkTrust

try with
| replace "44*" WITH "0*" IN telnofac
instead rex command
Bye.
Giuseppe

0 Karma

IRHM73
Motivator

Thank you @cusello

Regards

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Your Path to AgenticOps: AI Experiences for Every Splunk Practitioner

Your Path to AgenticOps: AI Experiences for Every Splunk Practitioner   Watch on demand us for a demo-driven ...

The Data Connection - October 2026

We all have data. Lots of it. But that’s not the problem – it’s understanding what we should we do with it, ...

Beyond the Acceleration Zone: Maximizing Your Success with Splunk Customer Experience ...

Customer success is built on tailored guidance and accessible resources. As a Splunk user, you are encouraged ...