An implementation of the Unix grep command-line utility written in Go.
Grepher is a tool that mimics the core functionality of the Unix grep utility. It allows you to search for patterns in files using regular expressions and provides several useful flags to customize your search.
This project demonstrates Go's powerful standard libraries and concise syntax while providing a useful tool for developers.
- Search for patterns using regular expressions
- Support for reading from files or standard input (stdin)
- Recursive directory searching
- Whole word matching
- Case-insensitive searching
- Line number display
- Match counting
- Invert match functionality
# Clone the repository
git clone https://github.com/yourusername/grepher.git
# Navigate to the project directory
cd gogrep
# Build the executable
go build -o gogrep
# Optional: Move to a directory in your PATH
mv gogrep /usr/local/bin/Basic syntax:
./gogrep [OPTIONS] PATTERN [FILE...]
If no file is specified, GoGrep reads from standard input.
| Flag | Description |
|---|---|
-i |
Perform case-insensitive matching |
-v |
Select non-matching lines (invert match) |
-n |
Prefix each line of output with its line number |
-c |
Only print a count of matching lines |
-r |
Recursively search subdirectories |
-w |
Match whole words only |
Grepher uses Go's regexp package to perform pattern matching on input files. It reads files line by line using bufio.Scanner, applies the specified matching criteria, and outputs the results.
The implementation includes:
- Command-line flag parsing with the
flagpackage - Regular expression compilation and matching
- File and directory handling
- Binary file detection (for recursive searches)
- Custom handling for special cases like stdin
Contributions are welcome! Feel free to submit issues or pull requests.
- Fork the repository
- Create your feature branch:
git checkout -b feature-name - Commit your changes:
git commit -m 'Add some feature' - Push to the branch:
git push origin feature-name - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.