Splunk Search

Use rex to remove leading zeros

bablucho
Path Finder

Regex:

Printed\s\s\s\s.(.+)

Test String:
Printed : 001727

Output:
1. 001727

I want the output to display without the leading zeros. so as it appears: 1727

Can anybody help?

Tags (2)
0 Karma

vnravikumar
Champion

Try this

| makeresults 
| eval test=0000123456000001200 
| rex field="test" mode=sed "s/(^0*)//1"

Result: 123456000001200

0 Karma

cpetterborg
SplunkTrust
SplunkTrust

If one of these answers has worked for you, please accept the answer, so that future viewers of this question can know that it has been answered for you, and which one worked for you.

Thanks!

0 Karma

saurabhkharkar
Path Finder
if you convert the string to integer, it will get rid of the leading zeros

| makeresults
| eval String="00147"
| eval StringInt = tonumber(String)
| table String StringInt

Else if you want to keep it as a string, you can try using this regex
| makeresults
| eval String="00147"
|rex field=String "(?<Output>[^0+]\S+)" 
| table String Output

cpetterborg
SplunkTrust
SplunkTrust

Probably something like:

Printed\D+0*(\d+)

should work. \D+ is all non-digits, followed by zero or more 0's, then it puts the other digits is the capture group.

0 Karma

Vijeta
Influencer

You can use ltrim function

|makeresults| eval x="001727"|eval x= ltrim(tostring(x),"0")
0 Karma

wenthold
Communicator

The rex string I'd use:

Printed[\s:]+[0]*(?<your_field>[0-9]+)
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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...