Operations 12 min read

Master Fast Log Error Detection with Tail, Grep, and Sed Commands

This guide shows how to quickly locate errors in massive server logs by using Linux commands such as tail, cat, grep, sed, head, and pagination tools, offering multiple practical techniques for pinpointing problematic entries.

Open Source Linux
Open Source Linux
Open Source Linux
Master Fast Log Error Detection with Tail, Grep, and Sed Commands

Why Log Analysis Matters

Viewing server and application logs is an essential skill for every programmer because logs are the best way to identify where errors occur. When a server crashes, knowing how to locate the error quickly can save you from a lot of trouble.

Dynamic Log Viewing

tail -f catalina.out

Continuously follows the end of the log file, showing new entries as they appear.

Open Log from the Beginning

cat catalina.out

Displays the entire content of the log file.

Redirect Output to a New File

cat -n catalina.out | grep "keyword" > nanjiangtest.txt

Searches for a keyword, shows line numbers, and saves the result to nanjiangtest.txt for further inspection.

Simple Tail/Head Commands

# Show the last <em>number</em> lines
tail -n number catalina.out
# Show lines after line <em>number</em>
tail -n +number catalina.out
# Show the first <em>number</em> lines
head -n number catalina.out
# Show all but the last <em>number</em> lines
head -n -number catalina.out

Method 1 – Find Line Numbers by Keyword

First obtain the line numbers of the relevant entries, then view surrounding lines. cat -n test.log | grep "keyword" After getting the line number, you can extract a range:

cat -n catalina.out | tail -n +13230539 | head -n 10

Method 2 – Search Within a Time Range

# Search for a specific timestamp
grep '11:07 18:29:20' catalina.out
# Use sed to extract a range between two timestamps
sed -n '/11:07 18:29:20/,/11:07 18:31:11/p' catalina.out

Method 3 – Count Occurrences of a String

grep '1175109632' catalina.out | wc -l

This returns the number of matching lines.

Method 4 – Tail Last number Lines and Grep Keyword

tail -n 20 catalina.out | grep 'INFO Takes:1'

Method 5 – Highlight Matches

tail -n 20 catalina.out | grep 'INFO Takes:1' --color

Method 6 – Show Context Lines

tail -n 20 catalina.out | grep 'INFO Takes:1' --color -a2

The -a2 option displays two lines of context before and after each match.

Method 7 – Paginate Results

# Use more for simple pagination
tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -a2 | more
# Use less for more control
tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -a2 | less

Additional Navigation Shortcuts (in less )

ctrl + F

– move forward one screen ctrl + B – move backward one screen ctrl + D – move forward half screen ctrl + U – move backward half screen j – move down one line k – move up one line G – go to the last line g – go to the first line q or ZZ – quit

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxcommand-linelog analysisGreptailsed
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.