Splunk Search

Display lines before and after the results of your string search

jtpryan
New Member

I want to do a specific string search, say "mary had a little lamb" and have it return the results including the 5 lines previous and the 5 lines after.

I have seen some (too complex to believe) results here, but all near 10 years old.  Is there a more recent, simpler way to do this?  It is a simple switch in grep.

 

Thanks

Labels (1)
0 Karma

to4kawa
Ultra Champion
index=yours [search "mary had a little lamb" | eval earliest=relative_time(_time,"-5s"), latest=relative_time(_time,"+5s") | fields earliest latest | format]

Unlike grep, you can only handle it in time.

Get Updates on the Splunk Community!

Data Management Digest – December 2025

Welcome to the December edition of Data Management Digest! As we continue our journey of data innovation, the ...

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...