Splunk Search

How to use eval to change a field's value?

guillecasco
Path Finder

Is it possible with EVAL do the following? I have a field named version which brings the value like this:

Version
60101228
50201315

but I would like to change it for the following (and maintain the original)

Version
" 60101228 or 6.1.1228"
"50201315 or 5.2.1315"

Where a 0 (zero) is replaced for a dot (.). I need this because later I will need both values in a dynamic drop-down search in which values can appear in both ways.

Can eval do this? Maybe other function? thanks!

Tags (2)
0 Karma
1 Solution

sundareshr
Legend

Try this

index=* usearch | rex "\"version\": \"(?\w*)\"" | dedup Version | eval version = replace(version,"0",".") | table Version

View solution in original post

nawazns5038
Builder

eval version2=replace(version,"0",".") | eval new_version= version+" or "+ version2 | rename new_version AS version.

This avoids confusion between field names .

0 Karma

jkat54
SplunkTrust
SplunkTrust
index=* usearch
|rex "\"version\": \"(?<major>\d)(?<minor1>\d{2})(?<minor2>\d{2})(<minor3>\d{3})\""  
|eval Version=major.".".minor1.".".minor2.".".minor3 
|eval Version=replace(Version,"0","")
|table Version
0 Karma

jkat54
SplunkTrust
SplunkTrust
index=* usearch 
|rex "\"version\": \"(?\w*)\"" 
|dedup Version
|rex field=Version mode=sed "s/(\d)(\d{2})(\d{2})(\d{3})/\1.\2.\3.\4/g" 
|eval Version=replace(Version,"0","") 
|table Version
0 Karma

sundareshr
Legend

Try this

index=* usearch | rex "\"version\": \"(?\w*)\"" | dedup Version | eval version = replace(version,"0",".") | table Version

sundareshr
Legend

If you want both formats, try this

| eval version=version." OR ".replace(version, "\.", "0")
0 Karma

robertlynch2020
Influencer

is there anyway to replace all non alphanumeric with a value?

0 Karma

somesoni2
Revered Legend

What is your use-case? Can you provide some sample of current and expected values?

0 Karma

robertlynch2020
Influencer

Thanks for your help

In Datamodels, i am trying to change the data i am reading in to have only alphanumeric

What i have
peter hi
ex⁢
field=1;like&
name
john

What i want
peterhi
exit
field1like
name
john

so i have been trying the following and it working for one, but i want one command for all non alphanumeric.
replace(Context,";","")
replace(Context,"=","")
replace(Context," ","")
replace(Context,"&","")

0 Karma

jkat54
SplunkTrust
SplunkTrust

Just use \W instead of each non-alphanumeric character you're currently using.

replace(Context,"\W","")

0 Karma

robertlynch2020
Influencer

This worked, thanks :).

However i have another issues where i am trying to do the same thing in props.conf
However i cant seem to get the REG to work

(?P<Elapsed>\w+)|

The idea is to change the Data at the initial point.

any help would be great 🙂

So the data is

20151029|12:31:00|MUREXFO   |     1 |SessionCreate                 |MXDIS..&PATCHER                  |   0.21s|   0.22s|100%|  -0.01s|   0% |                                      |1065.44Mb
20151029|12:31:00|MUREXFO   |     2 |RequestDocument3              |MXD~'##ISPATCHER                  |   0.01s|   0.03s|100%|  -0.02s|   0% |                                      |1065.65Mb
20151029|12:31:00|MUREXFO   |     3 |RequestDocument3              |MXDISP..??ATCHER                  |   0.01s|   0.01s|100%|   0.00s|   0% |       

The code i have is

^(?:[^\|\n]*\|){5}(?P<Command>\w+)| *-*(?P<Elapsed2>\d+\.\d+)\w+\| *-*(?P<CPU>\d+\.\d+)s\| *-*(?P<CPU_PER>\d+)%\| *-*(?P<RDB_COM>\d+\.\d+)s\| *-*(?P<RDB_COM_PER>\d+)%\s+\|

The issues is i am only getting the first few characters, and i want all alpha-numeric the pipe |

So i have
MXDIS..&PATCHER
MXD~'##ISPATCHER
MXDISP..??ATCHER

I want
MXDISPATCHER
MXDISPATCHER
MXDISPATCHER

0 Karma

jkat54
SplunkTrust
SplunkTrust

You should open a new question for this. Thanks!

0 Karma

jkat54
SplunkTrust
SplunkTrust

Use this regex for the match

\W

Only it will match underscore too.

0 Karma

somesoni2
Revered Legend

Try something like this.

| gentimes start=-1 | eval Version="60101228 50201315" | makemv Version | mvexpand Version | table Version 
| eval Version1=replace(Version,"^(\d)(\d{2})(\d{2})(\d+)","\1\2\3\4 OR \1.\2.\3.\4")  
| eval Version2=replace(Version1,"(\d+)\sOR\s(\d)\.0*([^\.]+)\.0*([^\.]+)\.([^\.]+)","\1 OR \2.\3.\4.\5")

Line 1 is just to generate sample data, replace it with your search
Line two gives a conversion (with leading 0s). If that works you can use just the line 2. If not, use both line 2 and line 3.

0 Karma

AshimaE
Explorer

@somesoni2 I tried a similar thing. On using two evals it is giving the error Error in 'eval' command: Regex: quantifier does not follow a repeatable item I need to need to replace two different original character sequences with the same replacement character.

0 Karma

guillecasco
Path Finder

how about the other way round?

version
1.2.3

to

version
1.2.3 or 10203

0 Karma

guillecasco
Path Finder

maybe I wasn't quite clear. This is the search and results.

index=* usearch |rex "\"version\": \"(?\w*)\"" |dedup Version |table Version

Version
60201327
60201528
60201827
60201429
50201219
50201413

now i would like to replace those value for :
Version
60201327 OR 6.2.1327
60201528 OR 6.2.1528

0 Karma

jkat54
SplunkTrust
SplunkTrust

It's exactly what he gave but slightly modified:

 index=* usearch |rex "\"version\": \"(?\w*)\"" |dedup Version
| eval Version1=replace(Version,"^(\d)(\d{2})(\d{2})(\d+)","\1\2\3\4 OR \1.\2.\3.\4")  
| eval Version=replace(Version1,"(\d+)\sOR\s(\d)\.0*([^\.]+)\.0*([^\.]+)\.([^\.]+)","\1 OR \2.\3.\4.\5")
| table Version
0 Karma

guillecasco
Path Finder

yes but how can i do it without the 0 in the second part? i have it like this:

Version
30201425 OR 3.02.01.425
30201424 OR 3.02.01.424

just need the 3.2.1.425

0 Karma

guillecasco
Path Finder

sorry i missunderstood, it´s working now, thanks!

0 Karma

jmallorquin
Builder

Hi,

Yes but with rex

|rex field=Version "(?<1>.)(?<2>.)(?<3>....)
| eval Version = 1.".".2.".".3

Hope i help you

0 Karma
Get Updates on the Splunk Community!

Aligning Observability Costs with Business Value: Practical Strategies

 Join us for an engaging Tech Talk on Aligning Observability Costs with Business Value: Practical ...

Mastering Data Pipelines: Unlocking Value with Splunk

 In today's AI-driven world, organizations must balance the challenges of managing the explosion of data with ...

Splunk Up Your Game: Why It's Time to Embrace Python 3.9+ and OpenSSL 3.0

Did you know that for Splunk Enterprise 9.4, Python 3.9 is the default interpreter? This shift is not just a ...