Security

How to compare 2 search results from different indexes and create a column that lists unique values?

fabrice
New Member

I tried the following query:

index=alldata Application="AZ"
|eval Date=strftime(_time,"%m/%d/%Y %H:%M:%S %Z")
|table Date user username |rename user as User, username as id |dedup id
|appendcols [search index=infor |fields disName userPrin |table disName userPrin |rename disName as Name userPrin as Mail |dedup Mail ]
|fields Date User id Mail Name
|eval "Login Status"=case(id==Mail, "Logged in", id!=Mail, "Never Logged in")
|eval Result=if(id=Mail, "Mail", "NULL")

I would like to create a column in the table that compares values in column id and Mail  and lists unique values (non duplicate).

0 Karma

fabrice
New Member

Thank you ITWhisperer! it worked

0 Karma

somesoni2
Revered Legend

Give this a try

index=alldata Application="AZ"
| stats max(_time) as _time by user username
|eval Date=strftime(_time,"%m/%d/%Y %H:%M:%S %Z")
|table Date user username |rename user as User, username as id 
| append [
    search index=infor |stats latest(disName) as Name by userPrin |rename userPrin as id 
]
| stats values(Date) as Date values(User) as User values(Name) as Name by id
|eval "Login Status"=case(isnotnull(Date), "Logged in", id!=Mail, "Never Logged in")
|eval Result=if(isnotnull(Date), "Mail", "NULL")
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

There is no guarantee that the events returned by the two searches have id and Mail in the same order or even the same number of events, and appendcols takes no account of any matches, it just added the first event from the second search to the first event of the first search, the second event of the second search to the second event of the first search and so on.

In order to get some sort of matching, you could use join (although this is not recommended for large data sets) or use stats with a by clause specifying the "join" field (you would have to rename one of the fields from one of the searches so it matches the name from the other search)

0 Karma

fabrice
New Member

Hi ITWhisperer,

Thank you for following up!

The id and Mail fields both have email adresses. the id field has active email and the Mail field list all email addresses (active and non active). I am looking to add a column in the table to list all non active emails.

Tags (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

index=alldata Application="AZ"
| eval Date=strftime(_time,"%m/%d/%Y %H:%M:%S %Z")
| rename user as User, username as id 
| dedup id
| eval status="Logged in"
| append [search index=infor 
  | fields disName userPrin
  | rename disName as Name userPrin as id
  | dedup id]
| stats values(Date) as Date values(User) as User values(Name) as Name values(status) as status by id
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!

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...

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