File: grawk.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 # grawk [options...] [awk expression...] [files...] 27 # 28 # 29 # GRoup via AWK groups lines using common results of the AWK expression given. 30 # When not given any, whole lines are used, which effectively puts identical 31 # lines next to each other. 32 # 33 # The handy case-insensitive shortcut options may cause this tool to fail, 34 # if the main AWK tool installed doesn't support the special IGNORECASE 35 # variable. 36 # 37 # The AWK options available only in single-dash versions are 38 # 39 # -F fs use `fs` for the field separator (the `FS` variable) 40 # -V show the AWK version installed 41 # -v var=val set a variable to a given value 42 # 43 # The other options are, available both in single and double-dash versions 44 # 45 # -h, -help show this help message 46 # -ins, -insensitive match regexes case-insensitively; fail if unsupported 47 # -tsv split fields using tabs, same as using -F "\t" 48 49 50 case "$1" in 51 -h|--h|-help|--help) 52 awk '/^# +grawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 53 exit 0 54 ;; 55 esac 56 57 tsv=0 58 case_insensitive=0 59 command='awk' 60 61 while [ $# -gt 0 ]; do 62 arg="$1" 63 64 if [ "${arg}" = "--" ]; then 65 shift 66 break 67 fi 68 69 case "${arg}" in 70 -F) 71 shift 72 if [ $# -eq 0 ]; then 73 printf "expected value after -F option\n" >&2 74 exit 1 75 fi 76 command="${command} -F $1" 77 shift 78 continue 79 ;; 80 81 -F*) 82 command="${command} ${arg}" 83 shift 84 continue 85 ;; 86 87 -v) 88 shift 89 if [ $# -eq 0 ]; then 90 printf "expected variable assignment after -v option\n" >&2 91 exit 1 92 fi 93 command="${command} -v $1" 94 shift 95 continue 96 ;; 97 98 -ins|--ins|-insensitive|--insensitive) 99 case_insensitive=1 100 shift 101 continue 102 ;; 103 104 -tsv|--tsv) 105 tsv=1 106 shift 107 continue 108 ;; 109 110 -*) 111 command="${command} ${arg}" 112 shift 113 continue 114 ;; 115 esac 116 117 break 118 done 119 120 code="${1:-\$0}" 121 [ $# -gt 0 ] && shift 122 123 # show all non-existing files given 124 failed=0 125 for arg in "$@"; do 126 if [ "${arg}" = "-" ]; then 127 continue 128 fi 129 if [ ! -e "${arg}" ]; then 130 printf "no file named \"%s\"\n" "${arg}" > /dev/stderr 131 failed=1 132 fi 133 done 134 135 if [ "${failed}" -gt 0 ]; then 136 exit 2 137 fi 138 139 ci=' 140 BEGIN { 141 if (IGNORECASE == "") { 142 m = "your `awk` command lacks case-insensitive regex-matching" 143 print(m) > "/dev/stderr" 144 exit 125 145 } 146 IGNORECASE = 1 147 } 148 ' 149 if [ "${case_insensitive}" -eq 0 ]; then 150 ci='' 151 fi 152 153 src="${ci}"' 154 BEGIN { if (SUBSEP == "") SUBSEP = "\034" } 155 156 FNR == 1 { FS = /\t/ ? "\t" : " "; $0 = $0 } 157 158 { 159 k = '"${code}"' 160 if (tally[k]++ == 0) ordkeys[++numkeys] = k 161 groups[k SUBSEP tally[k]] = $0 162 } 163 164 END { 165 for (i = 1; i <= numkeys; i++) { 166 k = ordkeys[i] 167 n = tally[k] 168 for (j = 1; j <= n; j++) print groups[k SUBSEP j] 169 } 170 } 171 ' 172 173 if [ "${tsv}" -eq 1 ]; then 174 ${command} -F "\t" "${src}" "$@" 175 else 176 ${command} "${src}" "$@" 177 fi