#!/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. # gsub [options...] [regexp-substitution pairs / regexps...] # # Global extended regex SUBstitute, using the AWK function of the same name: # arguments are used as regex-replacement pairs, in that order. Input lines # are always rid of ANSI-styles beforehand, if present. # # When run in erase/remove mode (see options below), arguments are all used # as regexes (instead of regex-substitution pairs), and all replacements are # empty strings. # # The options are, available both in single and double-dash versions # # -e, -erase, -r, -remove run in erase/remove mode # -h, -help show this help message # -i, -ins, -insensitive match regexes case-insensitively erase=0 case_insensitive=0 case "$1" in -h|--h|-help|--help) awk '/^# +gsub /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; -e|--e|-erase|--erase|-r|--r|-remove|--remove) erase=1 shift ;; -i|--i|-ins|--ins|-insensitive|--insensitive) case_insensitive=1 shift ;; esac [ "$1" = '--' ] && shift command='awk' if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then command='stdbuf -oL awk' fi ${command} -v ci="${case_insensitive}" -v erase="${erase}" ' BEGIN { if (ci && IGNORECASE == "") { m = "your `awk` command lacks case-insensitive regex-matching" print(m) > "/dev/stderr" exit 125 } if (ci) IGNORECASE = 1 for (i = 1; i < ARGC; i++) { args[++n] = ARGV[i] delete ARGV[i] if (erase) args[++n] = "" } } { # ignore cursor-movers and style-changers gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") # ignore OSC sequences gsub(/\x1b\][^\x1b]+\x1b\\/, "") for (i = 1; i <= n; i += 2) gsub(args[i], args[i + 1]) print } ' "$@"