A lightweight grep-like command-line utility built in Node.js.
It allows searching for patterns in files, directories (recursively), or standard input — mimicking core functionality of GNU grep.
-
Pattern Matching
- Supports simple substring search
- Character classes
[abc] - Escape sequences like
\d,\w - Anchors
^(start) and$(end) - Alternation
(word1|word2) - Quantifiers
+,? - Dot
.(any character)
-
Recursive Search
- With
-r, search entire directories and their subdirectories.
- With
-
Multiple File Support
- Handles one or many files, with file names prefixed in multi-file output.
-
Standard Input
- Works with piped input when no files are specified.
-
Exit Codes
0→ match found1→ no match found
Clone the repo and run directly with Node:
git clone https://github.com/yourusername/grep-clone.git
cd grep-clone
node grep.js -E pattern file.txtOptionally, make it globally accessible:
chmod +x grep.js
ln -s $(pwd)/grep.js /usr/local/bin/mygrepThen you can use:
mygrep -E foo file.txtnode grep.js -E <pattern> [files...]
node grep.js -r -E <pattern> [directories...]node grep.js -E hello file.txtMatches lines containing hello.
node grep.js -r -E "error" ./logsMatches all lines containing error across all files under logs/.
node grep.js -E "foo|bar" file.txtMatches lines containing either foo or bar.
node grep.js -E "^[0-9]+" numbers.txtMatches lines starting with a number.
node grep.js -E "[abc]" file.txtMatches lines containing a, b, or c.
echo "hello world" | node grep.js -E helloOutput: hello world
-
File traversal: Uses
fs.readdirSyncand recursion for-rto gather all files. -
Pattern Matching: Implemented via a custom
matchPatternfunction that:- Detects simple patterns (
includes) - Converts regex-like patterns into
RegExpobjects for matching - Handles anchors, alternation, and classes manually.
- Detects simple patterns (
-
I/O: Reads files line by line (
fs.readFileSync+ split on\n). -
CLI Arguments:
-E→ pattern matching mode (with regex-like syntax)-r→ recursive directory traversal
- Pure Node.js, no external dependencies.
- Mimics
grepbehavior for everyday use. - Recursive search with
-r. - Supports stdin for pipe workflows.
- Exit codes follow Unix conventions.