penguinpowernz/grut

A simple and efficient command-line tool for extracting chunks of text from files or stdin based on start and end patterns.

★ 0Forks 0GoGitHub ↗Compare

README

grut

A simple and efficient command-line tool for extracting chunks of text from files or stdin based on start and end patterns.

WARNING: AI SLOP

This code was generated by AI.

Features

  • Extract text chunks between start and end patterns (inclusive)
  • Read from files or stdin
  • Case-insensitive pattern matching with -i flag
  • Regular expression support with -P flag
  • Output to stdout or save to a file
  • Fast and memory-efficient line-by-line processing
  • Simple single-letter command-line flags

Installation

Build from source

# 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

Requirements

  • Go 1.16 or higher
  • Linux operating system

Usage

Basic usage

# From a file
grut -f input.txt -s "BEGIN" -e "END"

# From stdin
cat input.txt | grut -s "BEGIN" -e "END"

Command-line options

-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

Examples

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.txt

Case-insensitive matching:

grut -f document.txt -s "begin" -e "end" -i

Use 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]+" -P

Extract 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"

How it works

The tool scans through the input line by line and:

  1. Searches for the first line matching the start pattern
  2. Begins collecting all lines starting from the line with the start pattern
  3. Continues collecting lines until it finds the end pattern
  4. Returns all collected lines (including the lines with start and end patterns)

Both the start and end pattern lines are included in the output.

Pattern Matching

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.

Building

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 commands

Error handling

The tool will exit with an error message if:

  • Required flags (-s and -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

Name

"grut" is short for "grep cut"

License

MIT License - feel free to use this tool however you'd like!

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Contributors

penguinpowernz

Issues