#!/usr/bin/env bash PATTERN="eslint" FILES="$(fd .)" # Or use 'find .' to search recursively for file in $FILES; do if [[ -f "$file" ]]; then # 1. Use rg with line numbers (-n) and only the matched line (-o) # 2. Use awk to print ONLY the line number (field 1) # 3. Use xargs to pass multiple line numbers to a single sed command LINE_NUMBERS=$(rg --line-number --no-filename "$PATTERN" "$file" | awk -F':' '{print $1}' | tr '\n' ',') # Remove trailing comma if any LINE_NUMBERS=${LINE_NUMBERS%,} if [[ -n "$LINE_NUMBERS" ]]; then echo "Deleting lines $LINE_NUMBERS from $file..." # Use sed to delete the specified comma-separated line numbers in-place (-i) # NOTE: The syntax for -i might vary slightly between GNU sed (Linux) and BSD sed (macOS). sed -i.bak "${LINE_NUMBERS}d" "$file" # Optional: Remove the backup file created by sed -i.bak # rm "${file}.bak" else echo "$file: No lines matching pattern found." fi fi done