Splunk Search

Replace and add digit

jugarugabi
Path Finder

Hello, 

I have the following situation - in the original files I have the following information in the field:
ServerName1
ServerName10

I need to replace the values in the output to be something like:
srv01
srv10

while I know that the following command does replace the ServerName to srv, I cannot seem how to add 0 digit before the numbers that are having only one digit:

| rex field=AIS_ServerHost mode=sed "s/ServerName/srv/g"

The output will be:
srv1
srv10

A bit of help, please?

Thank you!

Labels (1)
0 Karma
1 Solution

manjunathmeti
SplunkTrust
SplunkTrust

hi @jugarugabi,
Apply rex command again to append 0 to values ending with a single digit.

| rex field=AIS_ServerHost mode=sed "s/ServerName/srv/g" 
| rex field=AIS_ServerHost mode=sed "s/srv(\d{1})$/srv0\1/g"

 

If this reply helps you, an upvote/like would be appreciated.

View solution in original post

jugarugabi
Path Finder

Yup, this did the trick. 

Thanks!

0 Karma

manjunathmeti
SplunkTrust
SplunkTrust

hi @jugarugabi,
Apply rex command again to append 0 to values ending with a single digit.

| rex field=AIS_ServerHost mode=sed "s/ServerName/srv/g" 
| rex field=AIS_ServerHost mode=sed "s/srv(\d{1})$/srv0\1/g"

 

If this reply helps you, an upvote/like would be appreciated.

Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...