Splunk Search

Getting nth line of a file

bleinfelder
Path Finder

Hi there,

I'm trying to monitor the output of a batch job with splunk. The problem is that logging is poorly implemented and I need some sophisticated splunk (?) to meet my requirements.

The job writes errors to a log file. Together with the error message it states the line of the input file where the error occured. The input file can be splunked as well.

To be able to handle the error the guys in business department need the error message as well as the corresponding line in the input file.

I can extract the message with the line number from the job's log file.

How can I extract the correct line from the input file?

So far I tried to parse each line of the input file as a different event and getting the nth one using streamstats. Unfortunately the result stream doesnt maintain the correct order of the lines.

Any ideas about how I could extract the nth line of the file?

Regards,

Bernd

Tags (1)
0 Karma

martin_mueller
SplunkTrust
SplunkTrust

You can select a specific line from an event like this:

sourcetype=input_file
| eval lines = _raw | makemv tokenizer="(.+)" lines
| eval error_line = mvindex(lines, [stats count | eval line_number = 4 | return $line_number])

I'm assuming your input file is indexed as one large event. The tokenizer turns the copy of _raw into a multivalue field, with each entry corresponding to one line. mvindex then accesses the line you need, as calculated by the subsearch that returns the line number from your error message.

bleinfelder
Path Finder

thank you, this works perfect!

0 Karma

richgalloway
SplunkTrust
SplunkTrust

When you say "the input file can be splunked as well" does that mean you have control over how it is ingested? If so, include the line number as a field with each line of input. Then you can easily associate the log entry with the input line.

---
If this reply helps you, Karma would be appreciated.
0 Karma

somesoni2
Revered Legend

sample logs please.

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!

Design, Compete, Win: Submit Your Best Splunk Dashboards for a .conf26 Pass

Hello Splunkers,  We’re excited to kick off a Splunk Dashboard contest! We know that dashboards are a primary ...

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...

Network to App: Observability Unlocked [May & June Series]

In today’s digital landscape, your environment is no longer confined to the data center. It spans complex ...