Fundamentals 5 min read

Master Linux grep: 10 Practical Examples to Boost Your Text Searching Skills

This guide introduces the powerful Linux grep command, explains its regex-based searching capabilities, and walks through ten hands‑on examples—including filtering by keywords, context lines, line numbers, and exclusion—followed by a concise reference of the most useful grep options.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux grep: 10 Practical Examples to Boost Your Text Searching Skills

The grep command is a versatile text‑search tool on Linux that uses regular expressions to match patterns and print matching lines. While it cannot replace text, it excels at quickly locating information; replacement tasks are handled by sed.

Practical Examples

The article provides a sample file (shown in the first image) and demonstrates ten common grep use‑cases. Each example includes a brief description and a screenshot of the command output.

Filter lines containing syslog.

Filter lines that start with ntp.

Show lines matching ntp plus the two following lines.

Show lines containing syslog together with the preceding line.

Show lines containing syslog plus both the previous and next lines.

Filter a specific keyword and display line numbers.

Filter lines that do not contain a keyword and show line numbers.

Remove empty lines.

Filter lines containing either root or syslog.

Search all files in the current directory for a keyword (very useful).

Quick Reference of Common Options

-A

: Show after N lines following a match. -B: Show before N lines preceding a match. -C: Show both before and after N lines. -c: Count the number of matching lines. -e: Combine multiple patterns with logical OR. -E: Enable extended regular expressions. -F: Fixed‑string search (equivalent to fgrep). -i: Ignore case distinctions. -n: Prefix each matching line with its line number. -o: Output only the matching part of the line. -q: Quiet mode; suppress output (useful in scripts). -s: Suppress error messages. -v: Invert match; show lines that do not match. -w: Match whole words only. --color: Highlight matches with color.

Related tools such as egrep and fgrep are essentially wrappers around grep with specific option presets, so mastering grep covers their functionality as well.

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.

LinuxregexGrepcommand-linetext search
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.