Project 3: A grep Clone

A simplified version of the grep command-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 try and catch for 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 try and catch keywords in the Zig documentation.
  • File I/O: std.fs and std.io modules.
  • Argument Parsing: std.process.args or std.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

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. 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