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 # -fade use a faded/gray style instead of highlighting 43 # -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 # -plain add variable `plain`, which is `$0` without ANSI-styles 47 # -tsv split fields using tabs, same as using -F "\t" 48 # -u underline (relevant) lines instead of highlighting them 49 50 51 cmd='awk' 52 ansi='7m' 53 buf=0 54 ins=0 55 plain_mode=0 56 tsv=0 57 58 while [ $# -gt 0 ]; do 59 case "$1" in 60 -b|--b|-buffered|--buffered) buf=1; shift; continue ;; 61 62 -F) 63 [ $# -ge 2 ] && cmd="${cmd} -F $2"; shift 2; continue 64 printf "expected value after -F option\n" >&2 65 exit 1 66 ;; 67 68 -F*) cmd="${cmd} $1"; shift; continue ;; 69 70 -fade|--fade|-faded|--faded) 71 ansi='38;2;168;168;168m'; shift; continue 72 ;; 73 74 -gray|--gray) ansi='48;2;216;216;216m'; shift; continue ;; 75 76 -h|--h|-help|--help) 77 awk '/^# +hawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 78 exit 0 79 ;; 80 81 -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;; 82 83 -plain|--plain) plain_mode=1; shift; continue ;; 84 85 -tsv|--tsv) tsv=1; shift; continue ;; 86 87 -u|--u|-underline|--underline|-underlined|--underlined) 88 ansi='4m'; shift; continue 89 ;; 90 91 -v) 92 [ $# -ge 2 ] && cmd="${cmd} -v $2" && shift 2 && continue 93 printf "expected variable assignment after -v option\n" >&2 94 exit 1 95 ;; 96 97 -) break ;; 98 99 --) shift; break ;; 100 101 -*) printf "unsupported option '%s'\n" "$1" >&2; exit 1 ;; 102 esac 103 104 break 105 done 106 107 cond="${1:-1}" 108 [ $# -gt 0 ] && shift 109 110 # show all non-existing files given 111 failed=0 112 for arg in "$@"; do 113 [ "${arg}" = "-" ] && continue 114 [ -e "${arg}" ] && continue 115 printf "no file named \"%s\"\n" "${arg}" >&2 116 failed=1 117 done 118 119 [ "${failed}" -gt 0 ] && exit 2 120 121 flush='' 122 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then 123 flush='fflush()' 124 fi 125 126 ci=' 127 BEGIN { 128 if (IGNORECASE == "") { 129 m = "your `awk` command lacks case-insensitive regex-matching" 130 print(m) > "/dev/stderr" 131 exit 125 132 } 133 IGNORECASE = 1 134 } 135 ' 136 [ "${ins}" -eq 0 ] && ci='' 137 138 plain=' 139 { 140 plain = $0 141 # ignore cursor-movers and style-changers 142 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain) 143 # ignore OSC sequences 144 gsub(/\x1b\][^\x1b]+\x1b\\/, "", plain) 145 } 146 ' 147 [ "${plain_mode}" -eq 0 ] && plain='' 148 149 src="${ci}"' 150 151 '"${plain}"' 152 153 ('"${cond}"') { 154 gsub(/\x1b\[0m/, "\x1b[0m\x1b['"${ansi}"'") 155 printf("\x1b['"${ansi}"'%s\x1b[0m\n", $0); '"${flush}"' 156 next 157 } 158 159 { print; '"${flush}"' } 160 ' 161 162 if [ "${tsv}" -eq 1 ]; then 163 ${cmd} -F "\t" "${src}" "$@" 164 else 165 ${cmd} "${src}" "$@" 166 fi