🔍 Linux/Unix: Searching, Text Processing & Regex
Estimated reading: 3 minutes 273 views

Linux/Unix: Text Search – grep, fgrep, egrep Explained with Examples

Introduction – Why Learn Text Search in Linux/Unix?

Text searching is one of the most powerful features of the Linux command line. Tools like grep, fgrep, and egrep allow you to search, filter, and extract information from massive files in seconds. These are essential for log analysis, pattern detection, and data parsing.

In this guide, you’ll learn:

  • The difference between grep, fgrep, and egrep
  • How to use regex (regular expressions) with grep
  • Practical examples for searching patterns, filtering logs, and combining options

What is grep?

Syntax:

grep [options] pattern [file...]

Description:

Searches for lines that match a given pattern in one or more files or from stdin.

Basic Example:

grep "error" logfile.txt

Finds lines containing the word “error”.


Common grep Options

OptionDescription
-iCase-insensitive search
-nShow matching line numbers
-vInvert match (exclude pattern)
-rRecursive search through directories
-lShow filenames with matches only
-cCount matching lines
-eSpecify multiple patterns
--colorHighlight matching patterns

Example:

grep -i "error" /var/log/syslog
grep -v "debug" logfile.txt        # Exclude lines with 'debug'

fgrep – Fixed String grep (No Regex)

Syntax:

fgrep [options] string [file...]

Description:

Searches for exact string matches. It does NOT interpret special regex characters like *, ., [ etc.

Example:

fgrep "a+b" file.txt

Matches the literal string a+b without treating + as regex.


egrep – Extended grep with Extended Regex

Syntax:

egrep [options] pattern [file...]

Description:

Supports extended regular expressions (ERE) like +, ?, |, () without needing to escape them.

Example:

egrep "cat|dog" animals.txt

Matches lines that contain either “cat” or “dog”.

Grouping:

egrep "(error|fail|warn)" logfile.txt

Regular Expressions in grep (Basic)

PatternMeaning
.Any single character
*Zero or more of the preceding char
^Start of line
$End of line
[]Character class
\Escape special characters

Example:

grep "^ERROR" log.txt     # Lines starting with "ERROR"
grep "user[0-9]" data.txt # Matches user0, user1…user9

Combining grep with Other Commands

Count matching lines:

grep -c "404" access.log

Find IPs and sort by frequency:

cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head

Recursive search for a keyword:

grep -rn "timeout" /etc/

Summary – Recap & Next Steps

Text search utilities like grep, fgrep, and egrep are powerful tools in any sysadmin or developer’s toolkit. They help you dig through logs, extract patterns, and automate validation—all directly from the command line.

Key Takeaways:

  • grep supports basic regular expressions and is extremely flexible.
  • fgrep is for fixed string searches (no regex).
  • egrep supports extended regex (like +, ?, |) without escaping.
  • Use options like -i, -v, -r, and --color for enhanced searching.
  • Combine with pipes for advanced pattern workflows.

FAQs

What’s the difference between grep, fgrep, and egrep?

  • grep: Supports basic regex
  • fgrep: No regex; searches plain strings
  • egrep: Supports extended regex like +, |, ()

How do I search recursively through folders?
Use:

grep -r "pattern" /path/to/folder

How can I highlight the search term?
Use:

grep --color=always "pattern" file.txt

How do I find lines that don’t contain a string?
Use:

grep -v "skipthis" file.txt

What replaces egrep and fgrep in modern systems?
GNU recommends using:

grep -E     # For egrep
grep -F     # For fgrep

Share Now :
Share

🔵 Linux/Unix: Text Search (grep, fgrep, egrep)

Or Copy Link

CONTENTS
Scroll to Top