File: hawk.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # hawk [options...] [awk condition...] [files...] 27 # 28 # 29 # Highlight (lines) satisfying an AWK condition/expression, keeping other 30 # lines the same. When not given a condition, all lines are highlighted. 31 # 32 # The handy case-insensitive shortcut options may cause this tool to fail, 33 # if the main AWK tool installed doesn't support the special IGNORECASE 34 # variable. 35 # 36 # The AWK options available only in single-dash versions are 37 # 38 # -F fs, -Ffs, -F=fs make `fs` the field separator 39 # 40 # The other options are, available both in single and double-dash versions 41 # 42 # -f, -fade use a faded/gray style instead of highlighting 43 # -g, -gray use a gray-background style instead of highlighting 44 # -h, -help show this help message 45 # -i, -ins match regexes case-insensitively; may fail the `awk` tool 46 # -tsv split fields using tabs, same as using -F "\t" 47 # -u underline (relevant) lines instead of highlighting them 48 49 50 cmd='awk' 51 ansi='7m' 52 buf=0 53 ins=0 54 tsv=0 55 56 while [ $# -gt 0 ]; do 57 case "$1" in 58 -b|--b|-buffered|--buffered) buf=1; shift; continue ;; 59 60 -F) 61 [ $# -ge 2 ] && cmd="${cmd} -F $2"; shift 2; continue 62 printf "expected value after -F option\n" >&2 63 exit 1 64 ;; 65 66 -F*) cmd="${cmd} $1"; shift; continue ;; 67 68 -f|--f|-fade|--fade|-faded|--faded) 69 ansi='38;2;168;168;168m'; shift; continue 70 ;; 71 72 -g|--g|-gray|--gray) ansi='48;2;216;216;216m'; shift; continue ;; 73 74 -h|--h|-help|--help) 75 awk '/^# +hawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 76 exit 0 77 ;; 78 79 -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;; 80 81 -tsv|--tsv) tsv=1; shift; continue ;; 82 83 -u|--u|-underline|--underline|-underlined|--underlined) 84 ansi='4m'; shift; continue 85 ;; 86 87 -v) 88 [ $# -ge 2 ] && cmd="${cmd} -v $2" && shift 2 && continue 89 printf "expected variable assignment after -v option\n" >&2 90 exit 1 91 ;; 92 93 -) break ;; 94 95 --) shift; break ;; 96 97 -*) printf "%s: unsupported option '%s'\n" "$0" "$1" >&2; exit 1 ;; 98 esac 99 100 break 101 done 102 103 cond="${1:-1}" 104 [ $# -gt 0 ] && shift 105 106 # show all non-existing files given 107 failed=0 108 for arg in "$@"; do 109 [ "${arg}" = "-" ] && continue 110 [ -e "${arg}" ] && continue 111 printf "%s: no file named \"%s\"\n" "$0" "${arg}" >&2 112 failed=1 113 done 114 115 [ "${failed}" -gt 0 ] && exit 2 116 117 flush='' 118 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then 119 flush='; fflush()' 120 fi 121 122 ci=' 123 BEGIN { 124 if (IGNORECASE == "") { 125 m = "your `awk` command lacks case-insensitive regex-matching" 126 print(m) > "/dev/stderr" 127 exit 125 128 } 129 IGNORECASE = 1 130 } 131 ' 132 [ "${ins}" -eq 0 ] && ci='' 133 134 src="${ci}"' 135 { 136 # keep original record/line to (maybe) emit it later 137 RECORD = $0 138 139 # ignore cursor-movers and style-changers 140 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 141 # ignore OSC sequences 142 gsub(/\x1b\][^\x1b]+\x1b\\/, "") 143 } 144 145 ('"${cond}"') { 146 gsub(/\x1b\[0m/, "\x1b[0m\x1b['"${ansi}"'", RECORD) 147 printf("\x1b['"${ansi}"'%s\x1b[0m%s", RECORD, ORS)'"${flush}"' 148 next 149 } 150 151 { printf("%s%s", RECORD, ORS)'"${flush}"' } 152 ' 153 154 if [ "${tsv}" -eq 1 ]; then 155 ${cmd} -F "\t" "${src}" "$@" 156 else 157 ${cmd} "${src}" "$@" 158 fi