Splunk Search

Using the value of a subsearch in main search

tomscott21
Engager

I am trying to create a search that gets the top value of a search and saves it to a variable:

| eval top=[| eval MB_in=bytes_in/1024/1024 | stats sum(MB_in) by c_ip | rename sum(MB_in) as "Total Megabytes in" | sort -"Total Megabytes in" | head 1 | eval topval=c_ip | return $topval]

I want to then use this value in the main search.

This is currently returning the following error:
Error in 'eval' command: Failed to parse the provided arguments. Usage: eval dest_key = expression.

Tags (2)
0 Karma
1 Solution

manjunathmeti
Champion

Value should be returned from a sub-search. Change your query to:

| eval top=[search <subsearch_query>| eval MB_in=bytes_out/1024/1024 | stats sum(MB_in) by c_ip | rename sum(MB_in) as "Total Megabytes Out" | sort -"Total Megabytes Out" | head 1 | eval topval=c_ip | return $topval]

If you are using top to filter base search results, then you can do this.

<base_search> top=[search <subsearch_query>| eval MB_in=bytes_out/1024/1024 | stats sum(MB_in) by c_ip | rename sum(MB_in) as "Total Megabytes Out" | sort -"Total Megabytes Out" | head 1 | eval topval=c_ip | return $topval]

View solution in original post

manjunathmeti
Champion

Value should be returned from a sub-search. Change your query to:

| eval top=[search <subsearch_query>| eval MB_in=bytes_out/1024/1024 | stats sum(MB_in) by c_ip | rename sum(MB_in) as "Total Megabytes Out" | sort -"Total Megabytes Out" | head 1 | eval topval=c_ip | return $topval]

If you are using top to filter base search results, then you can do this.

<base_search> top=[search <subsearch_query>| eval MB_in=bytes_out/1024/1024 | stats sum(MB_in) by c_ip | rename sum(MB_in) as "Total Megabytes Out" | sort -"Total Megabytes Out" | head 1 | eval topval=c_ip | return $topval]

tomscott21
Engager

I have run the first search, I am now being returned with the error "Error in 'eval' command: The number is invalid.", what does this mean?

0 Karma

manjunathmeti
Champion

It's type of the value is string then you need to format it:

| eval top=[search | eval MB_in=bytes_out/1024/1024 | stats sum(MB_in) by c_ip | rename sum(MB_in) as "Total Megabytes Out" | sort -"Total Megabytes Out" | head 1 | eval topval=c_ip | return $topval | format ]

you can simplify this query. Return command returns first row value by default.

| eval top=[search <subsearch_query> | stats sum(bytes_out) as "Total Bytes Out" by c_ip | sort -"Total Bytes Out" | return $c_ip | format ]
0 Karma

tomscott21
Engager

Amazing, so I now have a field called top in my main search - this is an IP. I am now trying to use this top value to filter the c_ip field. I have done this by searching for |search c_ip=top. This in not returning any results. am i filtering this in the correct way?

0 Karma

manjunathmeti
Champion

Then you don't need to format, you can filter main search like this.

| search c_ip=[search <subsearch_query> | stats sum(bytes_out) as "Total Bytes Out" by c_ip | sort -"Total Bytes Out" | return $c_ip ]
0 Karma

tomscott21
Engager

You are the splunk god, thank you!

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

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

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...