lazy-grep is a high-performance, line-oriented command-line tool for searching plain-text data sets for lines that match a regular expression. It is designed as a modern, Rust-based alternative to traditional grep utilities, with a focus on correctness and a rich set of features.
- Regex-based Search: Core functionality to search for complex patterns using regular expressions.
- Case-Insensitive Search: Use the
-iflag to perform case-insensitive matching. - Whole Word Search: Use the
-wflag to match only whole words. - Invert Match: Use the
-vflag to select non-matching lines. - Context Control: Display lines of context before (
-B), after (-A), or around (-C) each match. - Directory Traversal: Searches files in a given path recursively.
- Parallel Execution: Leverages the
ignorecrate to run searches in parallel for maximum speed. - Configurable Depth: Limit the directory traversal depth with the
--max-depthflag.
There are two ways to install lazy-grep.
Crate is published on crates.io, you can install it directly using cargo:
cargo install lazy-grepThis will download the source code, compile it, and place the executable in your Cargo binary path, making the lazy-grep command available globally on your system.
If you want to build from the source code, you must have the Rust toolchain installed.
-
Clone the repository:
git clone <repository-url> cd lazy-grep
-
Build the release executable:
cargo build --release
The executable will be available at
target/release/lazy-grep.
The basic syntax for lazy-grep is:
lazy-grep [OPTIONS] <PATTERN> <PATH>
-
Simple search in a file:
lazy-grep "error" log.txt -
Case-insensitive search in a directory:
lazy-grep -i "warn" ./logs/ -
Search for whole words only:
lazy-grep -w "complete" status.log -
Show 3 lines of context around each match:
lazy-grep -C 3 "critical" /var/log/ -
Show 5 lines after each match:
lazy-grep -A 5 "success" results.txt -
Invert the search to find lines that DO NOT contain "OK":
lazy-grep -v "OK" healthcheck.log
Performance tests were conducted against the system's standard grep utility. The test file was Herman Melville's Moby Dick (approx. 1.2 MB). The results below show the real time taken for each command.
Note: Benchmarks are indicative and can vary based on hardware and system load.
| Test | Pattern | lazy-grep (real time) |
grep (real time) |
|---|---|---|---|
| 1. Simple String | "whale" |
0.009s | 0.011s |
| 2. Case-Insensitive | "whale" |
0.008s | 0.011s |
3. Regex (\w*ness) |
"\b\w*ness\b" |
0.007s | 0.001s (grep -E) |
lazy-grep demonstrates competitive or superior performance on simple and case-insensitive string searches. The performance of grep on more complex regular expressions is notably faster, likely due to its highly optimized, mature regex engine.
- Language:
lazy-grepis written entirely in Rust, providing memory safety and high performance. - Argument Parsing: Command-line arguments are parsed using the excellent
clapcrate. - Regular Expressions: The powerful
regexcrate is used for all pattern matching. - Directory Traversal: File and directory handling is powered by the
ignorecrate, which provides parallel directory traversal and automatically respects.gitignorefiles. - Context Handling: The logic for before (
-B), after (-A), and surrounding (-C) context is implemented manually, with careful state management to handle overlapping or adjacent matches correctly.