Splunk Search

How can I join two searches on a common field?

AverageMale
Engager

This is my sample logs in [bowlers]:

"doYouBowl":"YES", "pin":"123", "name":"Billy"
"doYouBowl":"NO", "pin":"456", "name":"Bob"
"doYouBowl":"NO", "pin":"123", "name":"Mike"

Expected results is that the "pin" number must match and the resulting join results will be:

"doYouBowl":"YES", "pin":"123", "name":"Billy"
"doYouBowl":"NO", "pin":"123", "name":"Mike"

This is what I came up with by researching, but I get an error at 121-ish, where I try to join on pin and the second search:

 sourcetype="bowlers" \"doYouBowl\":\"NO\" | rex field=_raw "\"(?<pin>\d+)\"" | join pin [\"doYouBowl\":\"YES\" | rex field=_raw "\"(?<pin>\d+)\""]

Looking at the answers here, got suggestions to use transactions or (translate?), but want to get this join to work first and foremost.
Any assistance would be appreciated.

Thank you.

0 Karma
1 Solution

sundareshr
Legend

Try this

sourcetype="bowlers"  | rex field=_raw "\"pin\":\"(?<pin>\d+)\"" | rex "(?<bowl>YES|NO)"  | rex field=_raw "\"name\":\"(?<name>\w+)\"" | stats values(bowl) as doYouBowl values(name) as bowlers by pin | where mvcount(doYouBowl )=2

*UPDATED*

 sourcetype="bowlers"  | rex field=_raw "\"pin\":\"(?<pin>\d+)\"" | rex "(?<bowl>YES|NO)"  | rex field=_raw "\"name\":\"(?<name>\w+)\"" | eventstats values(bowl) as doYouBowl by pin | where mvcount(doYouBowl)=2

View solution in original post

somesoni2
Revered Legend

Another alternative

your base search | table _raw | extract kvdelim=":" pairdelim=", " | stats values(doYouBowl) as doYouBowl by pin | where mvcount(doYouBowl)=2
0 Karma

sundareshr
Legend

Try this

sourcetype="bowlers"  | rex field=_raw "\"pin\":\"(?<pin>\d+)\"" | rex "(?<bowl>YES|NO)"  | rex field=_raw "\"name\":\"(?<name>\w+)\"" | stats values(bowl) as doYouBowl values(name) as bowlers by pin | where mvcount(doYouBowl )=2

*UPDATED*

 sourcetype="bowlers"  | rex field=_raw "\"pin\":\"(?<pin>\d+)\"" | rex "(?<bowl>YES|NO)"  | rex field=_raw "\"name\":\"(?<name>\w+)\"" | eventstats values(bowl) as doYouBowl by pin | where mvcount(doYouBowl)=2

AverageMale
Engager

This appears to find the occurrences I am looking for but I have to drill down against each pin to confirm as the returned results are noted as:

1234567 NO/YES
5445435 NO/YES
...
...

Rather than the actual expected results noted in my original post:

"doYouBowl":"YES", "pin":"1234567", "name":"Billy"
"doYouBowl":"NO", "pin":"1234567", "name":"Mike"

Is there a way to get this results?

0 Karma

sundareshr
Legend

See updated query

0 Karma

AverageMale
Engager

Your last update seems to have done the trick! Thank you so much!

0 Karma

Richfez
SplunkTrust
SplunkTrust

Try making "pin" the field on all of it at once. No "join" is needed at that point, instead you can use a stats, transaction or other method to group them. Which is best all depends on what you are trying to do.

 sourcetype="bowlers"  | rex field=_raw "\"pin\":\"(?<pin>\d+)\"" | stats count by pin

You could transaction on pin, too, which would group the events a different way.

 sourcetype="bowlers"  | rex field=_raw "\"pin\":\"(?<pin>\d+)\"" | transaction maxspan=1d pin

Adjust your maxspan to be "long enough" but no longer.

You'll notice my rex "pins" the field so it'll look for the string "pin:" and the digits after that will be used as the field pin. This should make it more reliable in case you get other digits somewhere.

Ask if you need more, otherwise great question, thanks!

0 Karma

AverageMale
Engager

Hi Rich7177,

Sorry, I didn't emphasize on the major criteria that the query is to find all cases where records exists that have the same "pin" but different values for "doYouBowl"? This is why I wanted to use the join operation.

I tried your solutions the query just looked for where the row contained "pin" and reported stats on it regardless if "doYouBowl" had same or different values.

Any ideas?
Thanks.

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