#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # hawk [options...] [awk condition] [filenames...] # # # Highlight (lines) satisfying an AWK condition/expression, keeping other # lines the same. When not given a condition, all lines are highlighted. # # The handy case-insensitive shortcut options may cause this tool to fail, # if the main AWK tool installed doesn't support the special IGNORECASE # variable. # # The AWK options available only in single-dash versions are # # -F fs use `fs` for the field separator (the `FS` variable) # -V show the AWK version installed # -v var=val set a variable to a given value # # The other options are, available both in single and double-dash versions # # -h, -help show this help message # -ins, -insensitive match regexes case-insensitively; fail if unsupported # -tsv split fields using tabs, same as using -F "\t" # # -blue use a blue style # -gray use a gray style # -green use a green style # -orange use an orange style # -purple use a purple style # -red use a red style # # -bb, -blueback use a blue-background style # -grayback use a gray-background style # -gb, -greenback use a green-background style # -ob, -orangeback use an orange-background style # -pb, -purpleback use a purple-background style # -rb, -redback use a red-background style case "$1" in -h|--h|-help|--help) awk '/^# +hawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac ansi='7m' command='awk' if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then command='stdbuf -oL awk' fi tsv=0 case_insensitive=0 while [ $# -gt 0 ]; do arg="$1" if [ "${arg}" = "--" ]; then shift break fi case "${arg}" in -F) shift if [ $# -eq 0 ]; then printf "expected value after -F option\n" >&2 exit 1 fi command="${command} -F $1" shift continue ;; -F*) command="${command} ${arg}" shift continue ;; -v) shift if [ $# -eq 0 ]; then printf "expected variable assignment after -v option\n" >&2 exit 1 fi command="${command} -v $1" shift continue ;; -ins|--ins|-insensitive|--insensitive) case_insensitive=1 shift continue ;; -tsv|--tsv) tsv=1 shift continue ;; -blue|--blue) ansi='38;2;0;95;215m' shift continue ;; -bb|--bb|-blueback|--blueback) ansi='48;2;0;95;215m\x1b[38;2;238;238;238m' shift continue ;; -bold|--bold|-bolded|--bolded) ansi='1m' shift continue ;; -dim|--dim|-faint|--faint|-gray|--gray) ansi='38;2;168;168;168m' shift continue ;; -grayback|--grayback) ansi='48;2;168;168;168m\x1b[38;2;238;238;238m' shift continue ;; -green|--green) ansi='38;2;0;135;95m' shift continue ;; -gb|--gb|-greenback|--greenback) ansi='48;2;0;135;95m\x1b[38;2;238;238;238m' shift continue ;; -inv|--inv|-inverse|--inverse|-invert|--invert|-inverted|--inverted) ansi='7m' shift continue ;; -orange|--orange) ansi='38;2;215;95;0m' shift continue ;; -ob|--ob|-orangeback|--orangeback) ansi='48;2;215;95;0m\x1b[38;2;238;238;238m' shift continue ;; -purple|--purple) ansi='38;2;135;95;255m' shift continue ;; -pb|--pb|-purpleback|--purpleback) ansi='48;2;135;95;255m\x1b[38;2;238;238;238m' shift continue ;; -red|--red) ansi='38;2;204;0;0m' shift continue ;; -rb|--rb|-redback|--redback) ansi='48;2;204;0;0m\x1b[38;2;238;238;238m' shift continue ;; -strike|--strike|-striked|--striked|-stricken|--stricken) ansi='9m' shift continue ;; -underline|--underline|-underlined|--underlined) ansi='4m' shift continue ;; -*) command="${command} ${arg}" shift continue ;; esac break done cond="${1:-1}" [ $# -gt 0 ] && shift # show all non-existing files given failed=0 for arg in "$@"; do if [ "${arg}" = "-" ]; then continue fi if [ ! -e "${arg}" ]; then printf "no file named \"%s\"\n" "${arg}" > /dev/stderr failed=1 fi done if [ "${failed}" -gt 0 ]; then exit 2 fi ci=' BEGIN { if (IGNORECASE == "") { m = "your `awk` command lacks case-insensitive regex-matching" printf("\x1b[38;2;204;0;0m%s\x1b[0m\n", m) > "/dev/stderr" exit 125 } IGNORECASE = 1 } ' if [ "${case_insensitive}" -eq 0 ]; then ci='' fi src="${ci}"' '"${cond}"' { gsub(/\x1b\[0m/, "\x1b[0m\x1b['"${ansi}"'") printf "\x1b['"${ansi}"'%s\x1b[0m%s", $0, ORS next } 1 ' if [ "${tsv}" -eq 1 ]; then ${command} -F "\t" "${src}" "$@" else ${command} "${src}" "$@" fi