Operations 12 min read

Speed Up Log Searching with Powerful Grep Combos: A Live Demo

When a teammate struggled to find errors in massive Java service logs, the author demonstrated a step‑by‑step series of grep tricks—locking time and identifiers, chaining filters, using line numbers, context options, real‑time tailing, recursive search, and shell aliases—to turn chaotic log streams into precise, actionable insights.

ITPUB
ITPUB
ITPUB
Speed Up Log Searching with Powerful Grep Combos: A Live Demo

During a late‑night incident, a junior colleague was tail‑ing an 8 GB log file with tail -f and manually scanning for "ERROR" or "timeout" strings, which was painfully slow. The author stopped the command and asked three clarifying questions: which interface failed, roughly when, and whether a traceId or orderId was available.

After obtaining the traceId 7f2a9a0f1234567890abcdef, the author introduced the principle of "lock time + lock identifier" to narrow the log scope before grepping.

1. First trick: single keyword + line number

Extract all ERROR lines and pipe to head: grep "ERROR" app.log | head Then combine time, traceId, and line numbers:

grep "2025-12-26 20:10" app.log | grep "7f2a9a0f1234567890abcdef" -n

The -n flag prints line numbers, enabling later context extraction.

2. Second trick: layered multi‑keyword filtering

Filter ERROR, then the specific exception, then the traceId, finally exclude noisy retries:

grep "ERROR" app.log \
  | grep "OrderCreateException" \
  | grep -v "Retrying" \
  | grep "7f2a9a0f1234567890abcdef" -n

This pipeline isolates the real problem while discarding irrelevant retry logs.

3. Third trick: context options (-C/-A/-B)

Show five lines before and after a matching traceId to view the call chain: grep "7f2a9a0f1234567890abcdef" app.log -n -C 5 Variants -A 5 (after) and -B 5 (before) are also useful.

4. Fourth trick: real‑time monitoring with tail + grep

Watch live logs for a specific exception: tail -f app.log | grep "OrderCreateException" For robust tracking across log rotation, use:

tail -F app.log | stdbuf -oL grep "OrderCreateException"

5. Fifth trick: recursive directory search

Search all logs in a directory tree for a traceId:

grep -R "7f2a9a0f1234567890abcdef" /data/logs/app

When logs are split by date, combine --include and --exclude to avoid scanning gc or slow‑SQL logs:

grep -R "OrderCreateException" /data/logs/app \
  --include="app.log*" \
  --exclude="*gc.log"

To find the day with the most errors, pipe through awk, sort, and uniq:

grep "OrderCreateException" /data/logs/app/app.log.* \
  | awk '{print $1}' \
  | sort | uniq -c | sort -nr | head

6. Sixth trick: focus on Java stack traces

Show the exception line plus three following lines to locate the responsible method: grep -n "SocketTimeoutException" app.log -A 3 For a top‑N of failing methods, combine grep, awk, and sorting:

grep "Exception" app.log -A 1 \
  | grep "at com.demo" \
  | awk '{$1=""; print $0}' \
  | sort | uniq -c | sort -nr | head

7. Seventh trick: alias the common combos

Add shortcuts to ~/.bashrc for daily use:

# Search a whole day by traceId
alias glt='f(){ grep -R --color=auto "traceId=$1" /data/logs/app; }; f'
# Real‑time order errors
alias gerr='tail -f /data/logs/app/app.log | grep --color=auto "OrderCreateException"'

After reloading the shell ( source ~/.bashrc), the junior can run glt 7f2a9a0f1234567890abcdef to instantly retrieve the full call chain across all machines.

The root cause of the incident turned out to be an overly aggressive retry policy causing duplicate timeout requests, but without the grep combos the team would have been stuck scanning generic ERROR lines and guessing at the underlying subsystem.

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.

Linuxtroubleshootingcommand linelog analysisJava logginggreptail
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.