Splunk Search

How to extract values from rows that either contain Dept or do not contain Dept?

lctanlc
New Member
2015-05-01 07:33 - [User Login] | Name#ID | 'John#11' | :User name: 'John', ID: '11' successfully logged in
2015-05-02 17:07 - [Search] | Name#ID | 'Sue#222' | :Estimated time | '2,06ms' |, Result count | '23' |, Search Terms | '{"Type":"ALL","Tag":[],"sn":"","searchEndDate":null,"Query":"storm","searchStartDate":1428249600000}' |
2015-08-01 07:33 - [User Login] | Name#ID#Dept | 'Jim#333#ENG1' | :User name: 'Jim', ID: '333' successfully logged in
2015-08-03 09:18 - [Edit] | Name#ID#Dept | 'Tom#3333#ENG2' | :Announcement 'Maintenance' updated successfully.

I have a mixture of rows that contain Name#ID and Name#ID#Dept in a log file. For lines that do not contain Dept, I will have to look up to a CSV file. Otherwise, I will need to extract the Dept from the line.

I only know how to deal with Name#ID or Name#ID#Dept rows separately using the following search queries to extract the Name, ID and Dept:

... | rex "[^\[]*\[(?<Action>[^\]]*)\].*Name#ID \| '(?<Name>[^#]*)#(?<ID>[^']*)" | lookup LKUP.csv ID output Dept
... | rex "[^\[]*\[(?<Action>[^\]]*)\].*Name#ID#Dept \| '(?<Name>[^#]*)#(?<ID>[^']*)#(?<Dept>[^']*)"

Is there anyway to formulate a search query to combine the above-listed queries?

Tags (1)
0 Karma
1 Solution

woodcock
Esteemed Legend

Because lookups do not overwrite fields by default, you can do it like this:

... | rex "[^\[]*\[(?<Action>[^\]]*)\].*Name#ID#Dept \| '(?<Name>[^#]*)#(?<ID>[^']*)(?:#(?<Dept>[^']*))?" | lookup LKUP.csv ID output Dept

View solution in original post

0 Karma

woodcock
Esteemed Legend

Because lookups do not overwrite fields by default, you can do it like this:

... | rex "[^\[]*\[(?<Action>[^\]]*)\].*Name#ID#Dept \| '(?<Name>[^#]*)#(?<ID>[^']*)(?:#(?<Dept>[^']*))?" | lookup LKUP.csv ID output Dept
0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

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

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...