Splunk Search

How to strip out trailing 0's

efelder0
Communicator

I have a field in my output that contains the following values: DAT_Version = 6556.0000

What would the REGEX look like to strip out the .0000?

Tags (1)
0 Karma
1 Solution

bwooden
Splunk Employee
Splunk Employee

There are a few ways to do this using the search language, one is via the rex command to extract only numbers (everything left of the decimal) in your example:

... | rex field=DAT_Version "(?<DAT_Version>\d+)"

Another way is via eval to replace the decimal and all numbers to the right of it with nothing:

... | eval DAT_Version=replace(DAT_Version,"\.\d+","")

You may also choose to write a props to have this format extracted automatically.


MTA: You can also return the floor value, via eval:

... | eval DAT_Version=floor(DAT_Version)

View solution in original post

0 Karma

nick405060
Motivator

The other three answers here answer this use case specifically, that is, if there are nothing to the right of the decimal.

Here's how strip out trailing zeroes if you know you might have significant digits to the right of the decimal (e.g. "6556.123000"):

 | rex field=myfield"^(?<myfield>[\s\S]*\.[\s\S]*?)0*$" |

shandr
Path Finder

h/t Nick

I have iterated on your idea. It stripped the decimals nicely but kept the dot when "6556.000" so I added \d.

| rex field=alert_value "^(?<myfield>[\s\S]*\.\d[\s\S]*?)0*$"


In my case, my field also contains integers:

| rex field=alert_value "^(?<keep>[^\.]+)(?<keepdot>\.{0,1})(?<keepdotdecimal>\d*?)0*$"
| eval human_value = keep . if(len(keepdotdecimal)!=0, "." . keepdotdecimal, "")

It caters for "6556" and "6,556"

0 Karma

Masa
Splunk Employee
Splunk Employee

eval DAT_Version=round(DAT_Version, 0)

0 Karma

bwooden
Splunk Employee
Splunk Employee

There are a few ways to do this using the search language, one is via the rex command to extract only numbers (everything left of the decimal) in your example:

... | rex field=DAT_Version "(?<DAT_Version>\d+)"

Another way is via eval to replace the decimal and all numbers to the right of it with nothing:

... | eval DAT_Version=replace(DAT_Version,"\.\d+","")

You may also choose to write a props to have this format extracted automatically.


MTA: You can also return the floor value, via eval:

... | eval DAT_Version=floor(DAT_Version)
0 Karma

Drainy
Champion

I'm sure there is another eval magic trick that could do it but maybe something like;

| rex field=DAT_Version "(?<Datversion>[^.]+)"

Which will capture everything up until to the period

Also, if it helps / works then don't forget to accept the answer as right by clicking on the tick to the left! it means that others with the same questions will be able to find the right answers 🙂

0 Karma
Get Updates on the Splunk Community!

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

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

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...

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

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...