Operations 7 min read

Master grep, egrep, and fgrep: Essential Linux Text Filtering Techniques

This article explains the differences between grep, egrep, and fgrep, details common options and character classes, and demonstrates how to use regular‑expression metacharacters, grouping, and backreferences for powerful text filtering on Linux servers.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master grep, egrep, and fgrep: Essential Linux Text Filtering Techniques

1. Differences between grep, egrep, and fgrep

grep supports standard regular expressions, while egrep (or grep -e) supports extended regular expressions. fgrep does not support regular expressions at all, offering faster exact‑string matching.

2. Commonly used options

--color=auto : Enable syntax highlighting (default via alias).

-v : Invert match (select non‑matching lines).

-o : Show only the matched part of the line.

-i : Case‑insensitive matching.

-E : Use extended regular expressions (supported by egrep).

-A # : Show # lines after a match.

-B # : Show # lines before a match.

-C # : Show # lines surrounding a match.

3. Common character classes

[:digit:]

– all digits [:lower:] – all lowercase letters [:upper:] – all uppercase letters [:space:] – whitespace characters [:punct:] – punctuation symbols [:alpha:] – all alphabetic characters [:alnum:] – all letters and digits

4. Metacharacters in basic regular expressions

. : Matches any single character.

[] : Matches any one character inside the brackets.

[^] : Matches any character not listed inside the brackets.

* : Matches the preceding element zero or more times.

? : Matches the preceding element zero or one time.

{m,n} : Matches the preceding element at least m and at most n times.

{m} : Matches the preceding element exactly m times.

{0,n} : Matches up to n occurrences.

{m,} : Matches at least m occurrences.

^ : Anchors the match to the start of a line.

$ : Anchors the match to the end of a line.

\< : Anchors to the beginning of a word.

\> : Anchors to the end of a word.

\b : Word boundary (equivalent to \< or \>).

^$ : Matches an empty line.

5. Grouping, alternation, and backreferences

\( … \) : Groups a sub‑pattern, allowing it to be referenced later.

\n : Refers to the n ‑th captured group.

| : Alternation (logical OR) between patterns.

6. Extended regular expressions

Extended regex shares most metacharacters with basic regex but does not require escaping for most of them. The + quantifier matches the preceding element one or more times, making patterns more concise and readable.

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.

regexGrepText Filteringegrepfgrep
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.