File: hawk.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 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...] [filenames...] 27 # 28 # 29 # Highlight (lines) satisfying an AWK condition/expression, keeping all 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, if 33 # the main AWK tool installed doesn't support the special IGNORECASE variable. 34 # 35 # If /usr/bin/gawk is available, it becomes the AWK tool used: this ensures 36 # support for case-insensitive matching, along with many other extras. 37 # 38 # The AWK options available only in single-dash versions are 39 # 40 # -F fs use `fs` for the field separator (the `FS` variable) 41 # -V show the AWK/GAWK version installed 42 # -v var=val set a variable to a given value 43 # 44 # The other options are, available both in single and double-dash versions 45 # 46 # -h, -help show this help message 47 # 48 # -ins match regexes case-insensitively; fail if unsupported 49 # -insensitive match regexes case-insensitively; fail if unsupported 50 # 51 # -tsv split fields using tabs, same as using -F "\t" 52 # 53 # -blue use a blue style 54 # -gray use a gray style 55 # -green use a green style 56 # -orange use an orange style 57 # -purple use a purple style 58 # -red use a red style 59 # 60 # -bb, -blueback use a blue-background style 61 # -grayback use a gray-background style 62 # -gb, -greenback use a green-background style 63 # -ob, -orangeback use an orange-background style 64 # -pb, -purpleback use a purple-background style 65 # -rb, -redback use a red-background style 66 67 68 case "$1" in 69 -h|--h|-help|--help) 70 awk '/^# +hawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 71 exit 0 72 ;; 73 esac 74 75 ansi='7m' 76 command='awk' 77 if [ -e /usr/bin/gawk ]; then 78 command='/usr/bin/gawk' 79 fi 80 if [ -p /dev/stdout ] || [ -t 1 ]; then 81 command='stdbuf -oL awk' 82 fi 83 84 case_insensitive=0 85 86 while [ $# -gt 0 ]; do 87 arg="$1" 88 89 if [ "${arg}" = "--" ]; then 90 shift 91 break 92 fi 93 94 case "${arg}" in 95 -F) 96 shift 97 if [ $# -eq 0 ]; then 98 printf "expected value after -F option\n" >&2 99 exit 1 100 fi 101 command="${command} -F "$1"" 102 shift 103 continue 104 ;; 105 106 -F*) 107 command="${command} ${arg}" 108 shift 109 continue 110 ;; 111 112 -v) 113 shift 114 if [ $# -eq 0 ]; then 115 printf "expected variable assignment after -v option\n" >&2 116 exit 1 117 fi 118 command="${command} -v "$1"" 119 shift 120 continue 121 ;; 122 123 -ins|--ins|-insensitive|--insensitive) 124 case_insensitive=1 125 shift 126 continue 127 ;; 128 129 -tsv|--tsv) 130 command="${command} -F \"\\t\"" 131 shift 132 continue 133 ;; 134 135 -blue|--blue) 136 ansi='38;2;0;95;215m' 137 shift 138 continue 139 ;; 140 141 -bb|--bb|-blueback|--blueback) 142 ansi='48;2;0;95;215m\x1b[38;2;238;238;238m' 143 shift 144 continue 145 ;; 146 147 -bold|--bold|-bolded|--bolded) 148 ansi='1m' 149 shift 150 continue 151 ;; 152 153 -dim|--dim|-faint|--faint|-gray|--gray) 154 ansi='38;2;168;168;168m' 155 shift 156 continue 157 ;; 158 159 -grayback|--grayback) 160 ansi='48;2;168;168;168m\x1b[38;2;238;238;238m' 161 shift 162 continue 163 ;; 164 165 -green|--green) 166 ansi='38;2;0;135;95m' 167 shift 168 continue 169 ;; 170 171 -gb|--gb|-greenback|--greenback) 172 ansi='48;2;0;135;95m\x1b[38;2;238;238;238m' 173 shift 174 continue 175 ;; 176 177 -inv|--inv|-inverse|--inverse|-invert|--invert|-inverted|--inverted) 178 ansi='7m' 179 shift 180 continue 181 ;; 182 183 -orange|--orange) 184 ansi='38;2;215;95;0m' 185 shift 186 continue 187 ;; 188 189 -ob|--ob|-orangeback|--orangeback) 190 ansi='48;2;215;95;0m\x1b[38;2;238;238;238m' 191 shift 192 continue 193 ;; 194 195 -purple|--purple) 196 ansi='38;2;135;95;255m' 197 shift 198 continue 199 ;; 200 201 -pb|--pb|-purpleback|--purpleback) 202 ansi='48;2;135;95;255m\x1b[38;2;238;238;238m' 203 shift 204 continue 205 ;; 206 207 -red|--red) 208 ansi='38;2;204;0;0m' 209 shift 210 continue 211 ;; 212 213 -rb|--rb|-redback|--redback) 214 ansi='48;2;204;0;0m\x1b[38;2;238;238;238m' 215 shift 216 continue 217 ;; 218 219 -strike|--strike|-striked|--striked|-stricken|--stricken) 220 ansi='9m' 221 shift 222 continue 223 ;; 224 225 -underline|--underline|-underlined|--underlined) 226 ansi='4m' 227 shift 228 continue 229 ;; 230 231 -*) 232 command="${command} ${arg}" 233 shift 234 continue 235 ;; 236 esac 237 238 break 239 done 240 241 cond="${1:-1}" 242 [ $# -gt 0 ] && shift 243 244 # show all non-existing files given 245 failed=0 246 for arg in "$@"; do 247 if [ "${arg}" = "-" ]; then 248 continue 249 fi 250 if [ ! -e "${arg}" ]; then 251 printf "no file named \"%s\"\n" "${arg}" > /dev/stderr 252 failed=1 253 fi 254 done 255 256 if [ "${failed}" -gt 0 ]; then 257 exit 2 258 fi 259 260 ci=' 261 BEGIN { 262 if (IGNORECASE == "") { 263 m = "your `awk` command lacks case-insensitive regex-matching" 264 printf("\x1b[38;2;204;0;0m%s\x1b[0m\n", m) > "/dev/stderr" 265 exit 125 266 } 267 IGNORECASE = 1 268 } 269 ' 270 if [ "${case_insensitive}" -eq 0 ]; then 271 ci='' 272 fi 273 274 ${command} "${ci}"' 275 '"${cond}"' { 276 gsub(/\x1b\[0m/, "\x1b[0m\x1b['"${ansi}"'") 277 printf "\x1b['"${ansi}"'%s\x1b[0m%s", $0, ORS 278 next 279 } 280 1 281 ' "$@"