#!/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. # tla [options...] [awk expression...] [files...] # # Transform Lines with Awk expression: this is a convenient shortcut, to type # even less when using AWK. # # If a leading `=` (no quotes) is given as an argument, no inputs are read, # and the expression is evaluated/printed once, quitting right away. # # The 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 tab-separated values: split input fields using tabs case "$1" in -h|--h|-help|--help) awk '/^# +tla /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac tsv=0 buffered=0 case_ins=0 no_input=0 cmd='awk' while [ $# -gt 0 ]; do case "$1" in =) no_input=1; shift; continue ;; --b|-buffered|--buffered) buffered=1; shift; continue ;; -F) if [ $# -lt 2 ]; then printf "expected value after -F option\n" >&2 exit 1 fi cmd="${cmd} -F $2"; shift 2; continue ;; -F*) cmd="${cmd} $1"; shift; continue ;; -v) shift if [ $# -eq 0 ]; then printf "expected variable assignment after -v option\n" >&2 exit 1 fi cmd="${cmd} -v $1" shift continue ;; -i|--i|-ins|--ins|-insensitive|--insensitive) case_ins=1; shift; continue ;; -tsv|--tsv) tsv=1; shift; continue ;; -) break ;; --) shift; break ;; -*) printf "unsupported option '%s'\n" "$1" >&2 exit 1 ;; esac break done flush='' if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then flush='fflush()' fi expr="${1:-\$0}" [ $# -gt 0 ] && shift # show all non-existing files given failed=0 for arg in "$@"; do [ "${arg}" = "-" ] && continue [ -e "${arg}" ] && continue printf "no file named \"%s\"\n" "${arg}" >&2 failed=1 done [ "${failed}" -gt 0 ] && exit 2 ci=' BEGIN { if (IGNORECASE == "") { m = "your `awk` command lacks case-insensitive regex-matching" print(m) > "/dev/stderr" exit 125 } IGNORECASE = 1 } ' if [ "${case_ins}" -eq 0 ]; then ci='' fi if [ "${no_input}" -eq 1 ]; then src="${ci}"' BEGIN { print ('"${expr}"'); exit } ' else src="${ci}"' { print ('"${expr}"'); '"${flush}"' } ' fi if [ "${tsv}" -eq 1 ]; then ${cmd} -F "\t" -v OFS="\t" "${src}" "$@" else ${cmd} "${src}" "$@" fi