File: stawk.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2020-2025 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 # stawk [style] [awk condition...] [filenames...] 27 # 28 # 29 # STyle (lines) satisfying an AWK condition/expression, keeping all other 30 # lines the same. When not given a condition, all lines are styled. 31 # 32 # The colors/styles available are: 33 # blue 34 # bold 35 # gray 36 # green 37 # inverse 38 # magenta 39 # orange 40 # purple 41 # red 42 # underline 43 # 44 # Some style aliases are: 45 # b blue 46 # g green 47 # m magenta 48 # o orange 49 # p purple 50 # r red 51 # u underline 52 # hi inverse (highlight) 53 54 55 # handle leading options 56 case "$1" in 57 -h|--h|-help|--help) 58 awk '/^# +stawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 59 exit 0 60 ;; 61 esac 62 63 if [ $# -lt 1 ]; then 64 awk '/^# +stawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 65 exit 0 66 fi 67 68 name="${1:-invert}" 69 [ $# -gt 0 ] && shift 70 cond="${1:-1}" 71 [ $# -gt 0 ] && shift 72 73 awk -v name="${name}" ' 74 BEGIN { 75 # alias-lookup table 76 a["r"] = "red" 77 a["g"] = "green" 78 a["b"] = "blue" 79 a["o"] = "orange" 80 a["p"] = "purple" 81 a["m"] = "magenta" 82 a["h"] = "invert" 83 a["i"] = "invert" 84 a["u"] = "underline" 85 a["or"] = "orange" 86 a["ma"] = "magenta" 87 a["hi"] = "invert" 88 a["in"] = "invert" 89 a["un"] = "underline" 90 a["inv"] = "invert" 91 a["mag"] = "magenta" 92 a["grey"] = "gray" 93 a["inverse"] = "invert" 94 a["inverted"] = "invert" 95 a["hilite"] = "invert" 96 a["hilited"] = "invert" 97 a["highlight"] = "invert" 98 a["highlighted"] = "invert" 99 a["underlined"] = "underline" 100 a["bb"] = "blueback" 101 a["gb"] = "greenback" 102 a["mb"] = "magentaback" 103 a["ob"] = "orangeback" 104 a["pb"] = "purpleback" 105 a["rb"] = "redback" 106 a["bg"] = "greenback" 107 a["bm"] = "magentaback" 108 a["bo"] = "orangeback" 109 a["bp"] = "purpleback" 110 a["br"] = "redback" 111 a["bluebg"] = "blueback" 112 a["graybg"] = "grayback" 113 a["greenbg"] = "greenback" 114 a["greyback"] = "grayback" 115 a["greybg"] = "grayback" 116 a["magentabg"] = "magentaback" 117 a["orangebg"] = "orangeback" 118 a["purplebg"] = "purpleback" 119 a["redbg"] = "redback" 120 a["magback"] = "magentaback" 121 a["magbg"] = "magentaback" 122 a["orback"] = "orangeback" 123 a["orbg"] = "orangeback" 124 a["purback"] = "purpleback" 125 a["purbg"] = "purpleback" 126 127 # style-lookup table 128 s["red"] = "\x1b[38;2;204;0;0m" 129 s["green"] = "\x1b[38;2;0;135;95m" 130 s["blue"] = "\x1b[38;2;0;95;215m" 131 s["orange"] = "\x1b[38;2;215;95;0m" 132 s["purple"] = "\x1b[38;2;135;95;255m" 133 s["magenta"] = "\x1b[38;2;215;0;255m" 134 s["gray"] = "\x1b[38;2;168;168;168m" 135 s["bold"] = "\x1b[1m" 136 s["invert"] = "\x1b[7m" 137 s["italic"] = "\x1b[3m" 138 s["strike"] = "\x1b[9m" 139 s["underline"] = "\x1b[4m" 140 s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" 141 s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" 142 s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" 143 s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" 144 s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" 145 s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" 146 s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" 147 148 # resolve aliases 149 if (a[name] != "") { 150 name = a[name] 151 } 152 153 if (s[name] == "") { 154 fmt = "\x1b[31mstyle/color %s not supported\x1b[0m\n" 155 printf(fmt, name) > "/dev/stderr" 156 exit 1 157 } 158 159 style = s[name] 160 resetstyle = "\x1b[0m" style 161 } 162 163 { low = lower = tolower($0) } 164 165 '"${cond}"' { 166 gsub(/\x1b\[0m/, resetstyle) 167 printf "%s%s\x1b[0m\n", style, $0; fflush() 168 next 169 } 170 171 { print; fflush() } 172 ' "$@"