perg
A fast, feature-rich text search tool similar to grep, written in Rust.
perg is a modern implementation of the classic grep utility, designed for speed and ease of use. It supports regular expressions, recursive directory searching, and various output formatting options.
Features
- Fast text searching with regular expression support
- Recursive directory search with the
-rflag - Case-insensitive matching with the
-iflag - Line number display with the
-nflag - Filename display with the
-Hflag - Invert match (show non-matching lines) with the
-vflag - Files with/without matches listing with
-l/-Lflags - Count matching lines with the
-cflag - Show only matching parts with the
-oflag - Context lines with
-B(before),-A(after), and-C(around) flags - Limit matches with the
-mflag - Colorized output with the
--colorflag - Multiple file/directory support
- Proper error handling and exit codes
<PATTERN> Pattern )
)
Usage
A test.md file is included in this repo for testing:
Basic Usage
Search for a pattern in a file:
Advanced Features
Line numbers:
Case insensitive search:
Invert match (show lines that DON'T match):
Recursive directory search:
Show filenames with matches:
Show filenames without matches:
# (shows files that don't contain "world")
Count matching lines:
Show only matching parts:
Context lines around matches:
# Show 1 line before and after each match
# Show 2 lines of context around matches
Limit number of matches:
# Show only the first 2 matches
Colorized output:
# Show matches with color highlighting
# Never show colors (plain text)
# Show colors automatically based on terminal support (default)
Multiple files:
Regular expressions:
Combined options:
Installation
From Crates.io
Using Cargo via rustup:
|
Then install perg:
Note: If you see "cargo command not found", restart your terminal and run the install command again.
From Source
Clone the repository and build:
Exit Codes
0: Success, matches found (or no matches when using-L)1: File not found or other I/O errors2: Invalid regular expression
Project Structure
This project is organized as follows:
src/
├── main.rs # CLI entry point
├── lib.rs # Library exports
├── cli.rs # Command-line argument parsing
├── search.rs # Core search functionality
├── error.rs # Error types and handling
└── ...
Development
This project was created as a learning exercise for Rust programming. It demonstrates:
- Modern Rust development practices
- CLI application development with clap
- Error handling patterns
- Unit and integration testing
- Documentation with rustdoc
Running Tests
Benchmarks
The tool is optimized for performance and can handle large files and directories efficiently.
Contributing
Contributions are welcome! Please feel free to:
- Report bugs
- Suggest features
- Submit pull requests
- Improve documentation
License
This project is licensed under the MIT License - see the LICENSE file for details.
What's with the name?
Glad you asked, perg is just the reversed spelling of grep! 🦀
Rust Resources 🦀
- The Rust Programming Language Book
- Rust by Example
- Rustlings - Great for learning Rust
Rust continues to be one of the most loved programming languages according to Stack Overflow surveys. :gift: