Splunk Search

How to assign a string value from a subsearch to a eval which should map to all the events?

sudeshna_dash
New Member

I am trying to extract a value and add it to every events of that sourcetype.

source="c:\\splunk monitors\\log(2).txt"  | eval ServiceTag = [search source="c:\\splunk monitors\\log(2).txt" | head 1 |  rex field=_raw "^[^\[\n]*\[(?P<SvcTag>[^\]]+)" | eval tag = SvcTag | return $tag ]

Here $tag returns 5Q4RZH2 which is a string and thus, it is not able to get stored in ServiceTag variable of eval.
If the tag would have returned some number such as "123" then it is able to store the same.
E.g.

    source="c:\\splunk monitors\\log(2).txt"  | eval ServiceTag = [search source="c:\\splunk monitors\\log(2).txt" | head 1 |  rex field=_raw "^[^\[\n]*\[(?P<SvcTag>[^\]]+)" | eval tag = "123"| return $tag ]

In this scenario, ServiceTag has the value 123
Hence, it is getting difficult to store a string.

Can someone please solve?

0 Karma
1 Solution

HiroshiSatoh
Champion

Try this!

| eval tag = SvcTag | return $tag ]
↓
| eval tag = "\""+SvcTag+"\"" | return $tag ]

View solution in original post

niketn
Legend

@sudeshna_dash, are you doing it in a dashboard? Can you add some sample data and post the example above using code button?

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

HiroshiSatoh
Champion

Try this!

| eval tag = SvcTag | return $tag ]
↓
| eval tag = "\""+SvcTag+"\"" | return $tag ]

sudeshna_dash
New Member

Thanks a lot @HiroshiSatoh, this worked. I am grateful to u

0 Karma

mayurr98
Super Champion

Try

source="c:\\splunk monitors\\log(2).txt"  | eval ServiceTag =case( [search source="c:\\splunk monitors\\log(2).txt" | head 1 |  rex field=_raw "^[^\[\n]*\[(?P<SvcTag>[^\]]+)" | eval tag = SvcTag | return 10000 tag ],tag)

Let me know if this helps!

0 Karma

sudeshna_dash
New Member

Thanks @mayurr98 but this doesn't solve the problem

0 Karma
Get Updates on the Splunk Community!

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...