Grep Documentation
A comprehensive, gold-standard guide to mastering grep (Global Regular Expression Print). grep is the definitive UNIX tool for searching plain-text data sets for lines that match a regular expression. It is the backbone of text processing, log analysis, and shell pipelines.
Curriculum Architecture
This documentation is structured as a progressive curriculum, taking you from basic string matching to advanced regex-driven log parsing.
1. Introduction
Understand the origins of grep and its place in the modern ecosystem.
- What is Grep? - Core philosophy and architecture.
- Grep vs. Ripgrep - Tool selection for modern environments.
2. Core Matching
Master the basic mechanics of finding text.
- Basic Matching & Case - Literal strings and case insensitivity (
-i). - Word Boundaries - Preventing partial matches with exact word limits (
-w).
3. Regular Expressions
Unlock the true power of pattern matching.
- BRE vs. ERE - Understanding Basic vs. Extended Regular Expressions (
-E). - Common Regex Patterns - Anchors, character classes, and quantifiers.
4. Output & Context
Control what grep prints when a match is found.
- Context Lines - Printing before/after context with
-A,-B, and-C. - Formatting & Counting - Line numbers (
-n), file names (-l), and match counts (-c).
5. Files & Directories
Searching across complex filesystems.
- Recursive Search - Crawling directories with
-rand-R. - Include & Exclude - Filtering which files
grepactually reads.
6. Inversion & Logic
Flipping the search paradigm.
- Inverted Matching - Excluding lines with
-v. - AND / OR Logic - Simulating complex boolean logic in pipelines.
7. Grep Dialects
Navigating the historical variants of the tool.
- egrep, fgrep, and pcre - The differences between
-E,-F, and-P.
8. Performance & Limits
Optimizing grep for gigabyte-scale logs.
- Speed Optimizations -
LC_ALL=C, fixed strings, and memory considerations.
9. Real World Workflows
Production-ready patterns for system administrators.
- Log Parsing Pipelines - Extracting IPs, errors, and JSON data.
- Security Auditing - Searching codebases for secrets and vulnerabilities.
10. Troubleshooting
Diagnose common issues and edge cases.
- Binary Files & Encoding - Handling "binary file matches" errors.
- Escaping Nightmares - How to search for literal quotes, dashes, and regex metacharacters.
11. Cheatsheet
High-density reference for daily operations.
- Grep Cheatsheet - Core syntax, regex tables, and fast-lookup snippets.
grep is rarely used alone. Its true power is unlocked when you combine it with cat, tail, awk, and sed to filter data streams in real-time.