#!/bin/sh negate=0 mode="-a" # Process options while test $# -gt 0 && test "${1#-}" != "$1"; do case "$1" in -n) negate=1 ;; -a) mode="-a" ;; -o) mode="-o" ;; *) echo "Invalid option: $1" echo "usage: [options] arguments ..." echo " -a matching lines must contain all strings. the default" echo " -n negate" echo " -o matching lines must contain at least one of the strings" exit 1 ;; esac shift done # Ensure that patterns are provided if test $# -eq 0; then echo "No patterns provided." exit 1 fi # Build the regex based on the mode case "$mode" in -a) # Build regex with positive lookaheads for all patterns regex="" for pat in "$@"; do regex="${regex}(?=.*${pat})" done rg_opts="-P" ;; -o) # Build an OR regex for any of the patterns regex=$(printf '%s|' "$@" | sed 's/|$//') rg_opts="" ;; esac # Add the negate option if needed if test $negate -eq 1; then rg_opts="$rg_opts -v" fi # Run ripgrep with the constructed options and regex rg -S --color=never $rg_opts "$regex"