rgrep is a Ruby command-line utility designed to search plain-text data files for lines that match a pattern or regular expression. Inspired by the Unix grep
command, rgrep
offers flexible options for text searching and filtering. This utility can be used on both Unix-based systems and Windows, making it a portable and versatile tool for developers and system administrators.
rgrep
provides powerful pattern-matching capabilities using command-line options. It supports multiple search modes, including word search, regular expression search, and negated matching. Additional functionalities allow users to count or extract matches. With its simple interface, rgrep
is a handy tool for text processing tasks.
- 🔍 Search for specific words or patterns in files.
- 📜 Supports regular expressions for advanced pattern matching.
- ❌ Option to exclude lines that match a pattern.
- 📊 Count the number of matching lines.
- ✂️ Extract matching portions from the text.
- 🛠️ Built-in error handling for invalid options or arguments.
- Ensure Ruby is installed on your system. Ruby Installation Guide
- Clone or download the
rgrep
repository.
$ git clone https://github.com/kam-stand/rgrep-git
- Navigate to the directory where
rgrep.rb
is located.
$ cd rgrep
- Run the script using Ruby:
$ ruby rgrep.rb [options] <pattern> <filename>
Alternatively, you can make rgrep.rb
executable on a Unix-based system:
$ chmod +x rgrep.rb
$ ./rgrep.rb [options] <pattern> <filename>
Use the script directly:
$ path/to/rgrep/rgrep.rb [options] <pattern> <filename>
Option | Description | Notes |
---|---|---|
-w <pattern> |
Treats <pattern> as a word. Returns all lines containing the word. |
|
-p <pattern> |
Treats <pattern> as a regular expression. Returns all lines matching the regex. |
Default option. |
-v <pattern> |
Treats <pattern> as a regular expression. Returns all lines not matching the regex. |
|
-c <pattern> |
Used with -w , -p , or -v . Returns the number of lines that match the pattern. |
Must be used with -p . |
-m <pattern> |
Used with -w or -p . Returns the matched part of each line that matches the pattern. |
Must be used with -p . |
Search for the word "error" in a log file:
$ path/to/rgrep/rgrep.rb -w error /var/log/system.log
Search for lines containing an email address pattern:
$ path/to/rgrep/rgrep.rb -p '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' emails.txt
Find lines that do not contain the word "success":
$ path/to/rgrep/rgrep.rb -v success output.log
Count the number of lines that contain the word "warning":
$ path/to/rgrep/rgrep.rb -c -w warning /var/log/system.log
Extract email addresses from a text file:
$ path/to/rgrep/rgrep.rb -m -p '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' emails.txt
rgrep
includes robust error handling to ensure smooth operation and provide meaningful feedback to the user. Below are some common errors and how rgrep
handles them:
-
Invalid Option: If an unrecognized option is provided,
rgrep
will display an error message indicating the invalid option and provide a usage guide.$ path/to/rgrep/rgrep.rb -z pattern file.txt Error: Invalid option '-z' Usage: rgrep.rb [options] <pattern> <filename>
-
Missing Pattern: If no pattern is provided,
rgrep
will prompt the user to supply a pattern to search for.$ path/to/rgrep/rgrep.rb -w Error: Missing pattern Usage: rgrep.rb [options] <pattern> <filename>
-
Missing Filename: If no filename is provided,
rgrep
will prompt the user to supply a file to search in.$ path/to/rgrep/rgrep.rb -w pattern Error: Missing filename Usage: rgrep.rb [options] <pattern> <filename>
-
File Not Found: If the specified file does not exist,
rgrep
will display an error message indicating the file could not be found.$ path/to/rgrep/rgrep.rb -w pattern non_existent_file.txt Error: File 'non_existent_file.txt' not found
-
Invalid Regular Expression: If the provided regular expression is invalid,
rgrep
will display an error message indicating the issue with the regex.$ path/to/rgrep/rgrep.rb -p '[invalid_regex' file.txt Error: Invalid regular expression '[invalid_regex'
These error messages help users quickly identify and correct issues, ensuring a smoother experience with rgrep
.