Splunk Search

How to use rex in Calculated Fields?

mmagnuson
Engager

Hi,

I'm new to this forum and Splunk in general, so thank you in advance for all your help.
I'm trying to use rex in Calculated fields to extract some text and then further format it.
The rex itself looks like this:

rex field=source(?\d{4}-\d{2}) 

but when I pass it along to the Eval Expression field, I'm getting the following error message:

Encountered the following error while trying to save: In handler 'props-eval': Invalid operator

The regex works fine in Search.
Any help will be much appreciated.

0 Karma

Richfez
SplunkTrust
SplunkTrust

You can eval the results of extractions performed with rex, but you can't eval them in the rex itself (nor can you rex inside an eval).

Try it in two steps.

rex field=_raw "purple\s+widgets\s+(?<purplewidgets>\d+)\s+orange\s+widgets\s+(?<orangewidgets>\d+)" 
| eval totalwidgets=orangewidgets+purplewidgets

Assuming you have an event like

somedatetime, purple widgets 5 orange widgets 3

What pops out the other end will be, among some other fluff,

orangewidgets   purplewidgets   totalwidgets
3               5               8 

If you want to confirm, this is the exact, run-anywhere search you can test with.

| gentimes start="1/1/2016" end="1/2/2016" 
| eval myval="somedatetime, purple widgets 5 orange widgets 3"
| rex field=myval "purple\s+widgets\s+(?<purplewidgets>\d+)\s+orange\s+widgets\s+(?<orangewidgets>\d+)"
| eval totalwidgets=orangewidgets+purplewidgets

Gentimes just creates a single faked up "time" to work with.
I create an "event" to work with with the first eval.
The rex parses the field myval that I just created and extracts the digits for purple and orange widgets
The last eval does some math with them to find a total.

If instead you are trying to do a rex on a field you've created - well, as far as I know that works as long as it's a string and may work regardless. You can see I do that with "myval" above, in fact!

0 Karma

somesoni2
Revered Legend

The rex itself is a command, not a function that can be used within EVAL in search/ calculated fields. If you can provide some sample values and the formatting you're planning to do, we can suggest alternatives. My first guess will be the replace function which comes with eval.

swebb07g
Path Finder

Thank you. I was able to accomplish my goal using replace()

0 Karma

mmagnuson
Engager

Thank you somesoni2. In this case I think I can figure something out using substrings, I read somewhere it's possible to use rex with Calculated Fields, but I guess it's not 😉

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 ...