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