Splunk Search

How do I extract last numbers in field

dfigurello
Communicator

Hello Splunkers,

I Would like to create a new field with the last numbers in another field called logid

For example:

logid = 0101232010

logid = 0101242010
logid = 0101240210

I want create a new field with last 5 numbers, like as:

id= 32010
id= 42010
id= 40210

Could I create a regex for that?

Tks.

0 Karma
1 Solution

reed_kelly
Contributor

If I understand correctly, you just want to extract the last 5 characters of a field. Here is an example using the rex command:

|gentimes start=-1 |eval foo="1232134324" |rex field=foo "(?<id>\d{5})$" |table foo,id

returns: foo=1232134324 id=34324

View solution in original post

dfigurello
Communicator

Thank you Splunkers!

🙂

0 Karma

somesoni2
Revered Legend

Another options could be using substring.

sourcetype=fortigate subtype=admin pri=warning | stats count by log_id | eval newfield=substr(log_id,len(log_id)-4) | table log_id, newfield

reed_kelly
Contributor

If I understand correctly, you just want to extract the last 5 characters of a field. Here is an example using the rex command:

|gentimes start=-1 |eval foo="1232134324" |rex field=foo "(?<id>\d{5})$" |table foo,id

returns: foo=1232134324 id=34324

somesoni2
Revered Legend

Also, you've kept the name of new field same as old field, use following
...| rex field=log_id "(?\d{5})$" | table log_id,id

0 Karma

MuS
SplunkTrust
SplunkTrust

For a start keep it simple and just try to get the field=value pair like this:

sourcetype=fortigate subtype=admin pri=warning | rex "(?<log_id>\d{5})$" | table log_id

0 Karma

reed_kelly
Contributor

The backslash before the "d{5}" didn't get rendered. I have edited my answer to fix that. It should be "\d{5}"

0 Karma

dfigurello
Communicator

hi reed.kelly,

I don't get it. I am trying it into my search. Look at my seach:

sourcetype=fortigate subtype=admin pri=warning | stats count by log_id | rex field=log_id "(?d{5})$" | table log_id

Any idea ?

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!

Observability Simplified: Combining User Experience, Application Performance & ...

Tech Talk Observability Simplified: Combining User Experience, Application Performance & Network ...

Event Series May & June: From Network Visibility to Service Intelligence

Unifying the Network: Moving from Alert Noise to Service Intelligence with Splunk ITSI In today’s hybrid ...

Global Splunk User Group Events: May + June 2026

Your Splunk Community Awaits: Discover Upcoming User Group Events Worldwide    Staying ahead in the fast-paced ...