Splunk Search

How can I selectively append 0 on an attribute with a value less than 10 digits

leonheart78
Explorer

Hi there,

I'm handling a set of data which in one of the attributes, CustNo is inconsistent. I need to append "0" for those CustNo which are lesser than 10 digits.

Currently, I'm using "...... | eval CustNo= "0" + CustNo |" which is appending "0" to all the CustNo.
Thank you.

Leon

Tags (2)
0 Karma
1 Solution

jluo_splunk
Splunk Employee
Splunk Employee

If the field is a number already, you can use the len() function to check how many digits:
.... | eval check=if(len(date_second)>=10, date_second, date_second."0")

View solution in original post

woodcock
Esteemed Legend

Like this:

|rex field=CustNo mode=sed "s/^(\d{0,9})$/\1#0/ s/#//"
0 Karma

jluo_splunk
Splunk Employee
Splunk Employee

If the field is a number already, you can use the len() function to check how many digits:
.... | eval check=if(len(date_second)>=10, date_second, date_second."0")

aholzer
Motivator

As long as the field is a number this should work:
... | eval CustNo = if(CustNo<10, "0" . CustNo,CustNo)

If it isn't a number then you can do the following:
... | eval CustNo = if(tonumber(CustNo)<10, "0" . CustNo,CustNo)

Hope this helps

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!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...