Splunk Search

How to search between two lines?

runiyal
Path Finder

Hello All,

I have following lines in the log file -

 

Server8 runiyal 2023-01-12 09:48:41,880 INFO Plugin.DOCUMENT Bytes size from input stream : 2072823
server8 runiyal 2023-01-12 09:48:41,978 INFO Plugin.DOCUMENT File size after upload to temp folder: 2072823
server8 runiyal 2023-01-12 09:48:43,391 SUCCESS Plugin.DOCUMENT File size after notifying the docrepo : 2072823

 

I want to -
1. Search for the DocID in the end <2072823>; It should have SUCCESS written in line. (Line3)
2. It should then look at the above line with string "from input stream" for the same DocID (Line 1)
3. Reduce the timestamp from SUCCESS line (3) to the timestamp in line with the text "from input stream" (Line 1) - Result will be in seconds
4. Result should be in two columns: "DocID" and "Time Taken" (4)

Will appreciate your inputs on how this can be achieved. Thanks!

Labels (1)
0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @runiyal,

I suppose that you already extracted the DocID field, otherwise you have to add a rex command before the stats command.

you could use the transaction command

index=your_index 1. (SUCCESS OR "from input stream" OR "from input stream")
| transaction DocID
| rename duration AS "Time Taken"
| table DocID "Time Taken"

that's very slow, so try this different approach:

index=your_index 1. (SUCCESS OR "from input stream" OR "from input stream")
| stats 
   earliest(_time) AS earliest
   latest(_time) AS latest
   BY DocID
| eval Time_Take=latest-earliest
| table DocID Time_Taken
| rename Time_Taken AS "Time Taken"

Ciao.

Giuseppe

 

0 Karma

runiyal
Path Finder

Thanks Giuseppe.. I have to extract the docID field too.

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @runiyal,

in this case. you have to add a regex extraction to your search:

with transaction:

index=your_index 1. (SUCCESS OR "from input stream" OR "from input stream")
| rex "(?<DocID>\d+)$"
| transaction DocID
| rename duration AS "Time Taken"
| table DocID "Time Taken"

or with stats (better):

index=your_index 1. (SUCCESS OR "from input stream" OR "from input stream")
| rex "(?<DocID>\d+)$"
| stats 
   earliest(_time) AS earliest
   latest(_time) AS latest
   BY DocID
| eval Time_Take=latest-earliest
| table DocID Time_Taken
| rename Time_Taken AS "Time Taken"

You can test the regex at https://regex101.com/r/TgQtHA/1

Ciao.

Giuseppe

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Get Agentic with Splunk Lantern: Connect to Cisco Cloud Control, Transform ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...

July Community Events: Master ITSI 5.0 & Automate Splunk

Struggling with alert fatigue or feeling like you're spending more time on infrastructure maintenance than ...

New Release of Federated Search: Bringing Splunk Analytics to More of Your Data

Organizations today are generating more data than ever and storing it across cloud object stores, data lakes, ...