A simple and efficient command-line tool for extracting chunks of text from files or stdin based on start and end patterns.
This code was generated by AI.
- Extract text chunks between start and end patterns (inclusive)
- Read from files or stdin
- Case-insensitive pattern matching with
-iflag - Regular expression support with
-Pflag - Output to stdout or save to a file
- Fast and memory-efficient line-by-line processing
- Simple single-letter command-line flags
# Clone the repository
git clone https://github.com/yourusername/grut.git
cd grut
# Build the binary
make build
# (Optional) Install to system path
make install- Go 1.16 or higher
- Linux operating system
# From a file
grut -f input.txt -s "BEGIN" -e "END"
# From stdin
cat input.txt | grut -s "BEGIN" -e "END"-f string
Input file to process (uses stdin if not provided)
-s string
Start pattern (inclusive, required)
-e string
End pattern (inclusive, required)
-o string
Output file (optional, defaults to stdout)
-i
Case insensitive pattern matching (default: false)
-P
Treat patterns as regular expressions (default: false)
-v
Show version information
Extract a chunk from a file:
grut -f document.txt -s "<!-- START -->" -e "<!-- END -->"Read from stdin:
cat log.txt | grut -s "ERROR" -e "---"
echo "some text" | grut -s "START" -e "END"Save output to a file:
grut -f document.txt -s "BEGIN" -e "END" -o extracted.txtCase-insensitive matching:
grut -f document.txt -s "begin" -e "end" -iUse regular expressions:
# Match lines starting with "function" and ending with closing brace
grut -f code.js -s "^function.*{" -e "^}" -P
# Extract section with numbered headers
grut -f readme.md -s "^## [0-9]+" -e "^## [0-9]+" -PExtract code between markers:
grut -f source.py -s "# BEGIN FUNCTION" -e "# END FUNCTION"Pipe through other commands:
# Extract and count lines
cat file.txt | grut -s "START" -e "END" | wc -l
# Extract and search within chunk
grut -f log.txt -s "2024-01-01" -e "2024-01-02" | grep "ERROR"The tool scans through the input line by line and:
- Searches for the first line matching the start pattern
- Begins collecting all lines starting from the line with the start pattern
- Continues collecting lines until it finds the end pattern
- Returns all collected lines (including the lines with start and end patterns)
Both the start and end pattern lines are included in the output.
String matching (default): Patterns are treated as literal strings and matched using substring search.
Regular expressions (-P flag): Patterns are compiled as Go regular expressions (RE2 syntax).
Case sensitivity: By default, matching is case-sensitive. Use -i for case-insensitive matching.
The project includes a Makefile with several useful targets:
make build # Build the binary
make clean # Remove build artifacts
make install # Install to /usr/local/bin (requires sudo)
make uninstall # Remove from /usr/local/bin (requires sudo)
make test # Run basic tests
make help # Show available commandsThe tool will exit with an error message if:
- Required flags (
-sand-e) are missing - Input file cannot be opened (when using
-f) - Regular expression patterns are invalid (when using
-P) - Start pattern is not found in the input
- End pattern is not found after the start pattern
"grut" is short for "grep cut"
MIT License - feel free to use this tool however you'd like!
Contributions are welcome! Please feel free to submit a Pull Request.