Created
May 29, 2025 02:36
-
-
Save luccabb/ee5d67858ccfaaca68453ee1bc46e52c to your computer and use it in GitHub Desktop.
search prefixes on files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| if [[ $# -lt 3 ]]; then | |
| echo "Usage: $0 prefix1 [prefix2 …] -- file1 [file2 …]" >&2 | |
| exit 1 | |
| fi | |
| # collect prefixes until “--” | |
| prefixes=() | |
| while [[ $1 != -- ]]; do | |
| prefixes+=("$1") | |
| shift | |
| done | |
| shift # skip the -- | |
| # the rest are files | |
| if [[ $# -eq 0 ]]; then | |
| echo "Error: no files specified after --" >&2 | |
| exit 1 | |
| fi | |
| files=("$@") | |
| # for each prefix, scan all files with awk | |
| for prefix in "${prefixes[@]}"; do | |
| # escape any awk‐regex-special chars | |
| esc_pref=$(printf '%s' "$prefix" | sed 's/[.[\*^$(){}+?|\\]/\\&/g') | |
| awk -v p="$esc_pref" ' | |
| { | |
| line = $0 | |
| # find each occurrence of p | |
| while ((pos = index(line, p)) > 0) { | |
| rest = substr(line, pos + length(p)) | |
| # match up to next slash | |
| if (match(rest, /^[^\/]+/)) { | |
| print substr(rest, RSTART, RLENGTH) | |
| # advance past this match | |
| line = substr(rest, RSTART + RLENGTH) | |
| } else { | |
| break | |
| } | |
| } | |
| }' "${files[@]}" | |
| done \ | |
| | sort -u |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment