Splunk Search

substr can't get the correct value?

flora123
Path Finder

Hi,all

I want to use "substr" to get what I want.

A=1420014

... |eval A=if(substr(A, 1,2)="14",replace(A, "14", "0"),A) |table A

A will become "02000".

But it should be "020014".

Could anyone tell me what's worng? Can't I use substr by this way?

Thanks a lot. 😃

Tags (1)
1 Solution

dwaddle
SplunkTrust
SplunkTrust

The replace operator will replace every occurence of a value in a string. From http://www.splunk.com/base/Documentation/latest/SearchReference/CommonEvalFunctions :

This function returns a string formed by substituting string Z for every occurrence of regex string Y in string X. The third argument Z can also reference groups that are matched in the regex.

Given your example above, there are a couple of ways to attack it. If you're always wanting to replace the "14" (or whatever) at the beginning, you should be able to take advantage of the fact that replace uses regexes, and do something like:

... | eval A=if(substr(A,1,2)="14",replace(A,"^14","0"),A) | table A

Or you can accomplish the same thing with string concatenation, similar to:

... | eval A=if(substr(A,1,2)="14", "0".substr(A,3), A) | table A

View solution in original post

0 Karma

dwaddle
SplunkTrust
SplunkTrust

The replace operator will replace every occurence of a value in a string. From http://www.splunk.com/base/Documentation/latest/SearchReference/CommonEvalFunctions :

This function returns a string formed by substituting string Z for every occurrence of regex string Y in string X. The third argument Z can also reference groups that are matched in the regex.

Given your example above, there are a couple of ways to attack it. If you're always wanting to replace the "14" (or whatever) at the beginning, you should be able to take advantage of the fact that replace uses regexes, and do something like:

... | eval A=if(substr(A,1,2)="14",replace(A,"^14","0"),A) | table A

Or you can accomplish the same thing with string concatenation, similar to:

... | eval A=if(substr(A,1,2)="14", "0".substr(A,3), A) | table A
0 Karma

flora123
Path Finder

Wonderful!Thanks a lot.

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!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...