Skip to content

lazy-toad/lazy-grep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lazy-grep

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.

Features

  • Regex-based Search: Core functionality to search for complex patterns using regular expressions.
  • Case-Insensitive Search: Use the -i flag to perform case-insensitive matching.
  • Whole Word Search: Use the -w flag to match only whole words.
  • Invert Match: Use the -v flag 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 ignore crate to run searches in parallel for maximum speed.
  • Configurable Depth: Limit the directory traversal depth with the --max-depth flag.

Installation

There are two ways to install lazy-grep.

From Crates.io (Recommended)

Crate is published on crates.io, you can install it directly using cargo:

cargo install lazy-grep

This 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.

From Source

If you want to build from the source code, you must have the Rust toolchain installed.

  1. Clone the repository:

    git clone <repository-url>
    cd lazy-grep
  2. Build the release executable:

    cargo build --release

    The executable will be available at target/release/lazy-grep.

Usage

The basic syntax for lazy-grep is:

lazy-grep [OPTIONS] <PATTERN> <PATH>

Examples

  • 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

Benchmarks

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)

Observations

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.

Implementation Details

  • Language: lazy-grep is written entirely in Rust, providing memory safety and high performance.
  • Argument Parsing: Command-line arguments are parsed using the excellent clap crate.
  • Regular Expressions: The powerful regex crate is used for all pattern matching.
  • Directory Traversal: File and directory handling is powered by the ignore crate, which provides parallel directory traversal and automatically respects .gitignore files.
  • 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.

About

A recursive grep written in Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages