Splunk Search

How to use rex and ltrim to extract this field in my data?

kiran331
Builder

Hi,

I have a field EMP, I need to remove the 0000 present before the field, is this do able?
like, I'm using Rex and ltrim

|rex field=EMP "(?\d{5})$"|eval EMP=if (like (EMP,"0000%"),ltrim(EMP,"0000\\"),EMP)

EMP:
000012345
000000002(its removing all 0's but i need 00001)
0000ABCDEFGH

Expected output;
12345
00002
ABCDEFGH

0 Karma
1 Solution

mpreddy
Communicator

try something like this,

|stats c |eval f="000012345,000000002,0000ABCDEFGH,100000" |eval field=split(f,",") | fields - f | mvexpand field |rex mode=sed field=field  "s/^(0000)(.*)/\2/g"

View solution in original post

aaraneta_splunk
Splunk Employee
Splunk Employee

@kiran331 - Did one of the answers below help provide a solution your question? If yes, please click “Accept” below the best answer to resolve this post and upvote anything that was helpful. If no, please leave a comment with more feedback. Thanks.

0 Karma

woodcock
Esteemed Legend

Like this:

.... | rex field=EMP mode=sed "s/^0000//"
0 Karma

DalJeanis
Legend

This makes test data -

| makeresults | eval EMP="000012345 0000ABC 000000002 george" | makemv EMP| mvexpand EMP 

this answers the question

| eval EMP2=if (like (EMP,"0000%"),substr(EMP,5,len(EMP)-4),EMP)
| table EMP EMP2 

With this result

EMP         EMP2
000012345   12345
0000ABC     ABC
000000002   00002
george      george
0 Karma

mpreddy
Communicator

try something like this,

|stats c |eval f="000012345,000000002,0000ABCDEFGH,100000" |eval field=split(f,",") | fields - f | mvexpand field |rex mode=sed field=field  "s/^(0000)(.*)/\2/g"
Get Updates on the Splunk Community!

Stay Connected: Your Guide to January Tech Talks, Office Hours, and Webinars!

What are Community Office Hours? Community Office Hours is an interactive 60-minute Zoom series where ...

[Puzzles] Solve, Learn, Repeat: Reprocessing XML into Fixed-Length Events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Data Management Digest – December 2025

Welcome to the December edition of Data Management Digest! As we continue our journey of data innovation, the ...