Project 3: A grep Clone
A simplified version of the
grepcommand-line tool that searches for a pattern in files and prints matching lines.
Quick Reference
| Attribute | Value |
|---|---|
| Primary Language | Zig |
| Alternative Languages | C, Go, Rust |
| Difficulty | Level 1: Beginner |
| Time Estimate | Weekend |
| Knowledge Area | File I/O / CLI Tools |
| Tooling | A command-line search utility |
| Prerequisites | None. This is a great starting point. |
What You Will Build
A simplified version of the grep command-line tool that searches for a pattern in files and prints matching lines.
Why It Matters
This project builds core skills that appear repeatedly in real-world systems and tooling.
Core Challenges
- Parsing command-line arguments → maps to using
std.process.args - Reading files line by line → maps to buffered I/O and handling
\n - Handling I/O errors robustly → maps to using
tryandcatchfor file-not-found, permission-denied, etc. - Managing memory for file contents → maps to using an allocator to hold lines or the whole file
Key Concepts
- Error Handling: The
tryandcatchkeywords in the Zig documentation. - File I/O:
std.fsandstd.iomodules. - Argument Parsing:
std.process.argsorstd.options.
Real-World Outcome
$ cat file.txt
hello world
zig is cool
goodbye world
$ ./zig-grep world file.txt
hello world
goodbye world
Implementation Guide
- Reproduce the simplest happy-path scenario.
- Build the smallest working version of the core feature.
- Add input validation and error handling.
- Add instrumentation/logging to confirm behavior.
- Refactor into clean modules with tests.
Milestones
- Milestone 1: Minimal working program that runs end-to-end.
- Milestone 2: Correct outputs for typical inputs.
- Milestone 3: Robust handling of edge cases.
- Milestone 4: Clean structure and documented usage.
Validation Checklist
- Output matches the real-world outcome example
- Handles invalid inputs safely
- Provides clear errors and exit codes
- Repeatable results across runs
References
- Main guide:
LEARN_ZIG_DEEP_DIVE.md - “The Zig Programming Language” (Book) - Chapter on I/O