File: gsub.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 # gsub [options...] [regexp-substitution pairs / regexps...]
  27 #
  28 # Global extended regex SUBstitute, using the AWK function of the same name:
  29 # arguments are used as regex-replacement pairs, in that order. Input lines
  30 # are always rid of ANSI-styles beforehand, if present.
  31 #
  32 # When run in erase/remove mode (see options below), arguments are all used
  33 # as regexes (instead of regex-substitution pairs), and all replacements are
  34 # empty strings.
  35 #
  36 # The options are, available both in single and double-dash versions
  37 #
  38 #   -e, -erase, -r, -remove    run in erase/remove mode
  39 #   -h, -help                  show this help message
  40 #   -i, -ins, -insensitive     match regexes case-insensitively
  41 
  42 
  43 erase=0
  44 buffered=0
  45 case_insensitive=0
  46 
  47 while [ $# -gt 0 ]; do
  48     case "$1" in
  49         -b|--b|-buffered|--buffered)
  50             buffered=1
  51             shift
  52             continue
  53         ;;
  54 
  55         -e|--e|-erase|--erase|-r|--r|-remove|--remove)
  56             erase=1
  57             shift
  58             continue
  59         ;;
  60 
  61         -h|--h|-help|--help)
  62             awk '/^# +gsub /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  63             exit 0
  64         ;;
  65 
  66         -i|--i|-ins|--ins|-insensitive|--insensitive)
  67             case_insensitive=1
  68             shift
  69             continue
  70         ;;
  71     esac
  72 
  73     break
  74 done
  75 
  76 [ "$1" = '--' ] && shift
  77 
  78 flush=0
  79 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
  80     flush=1
  81 fi
  82 
  83 awk -v flush="${flush}" -v ci="${case_insensitive}" -v erase="${erase}" '
  84     BEGIN {
  85         if (ci && IGNORECASE == "") {
  86             m = "your `awk` command lacks case-insensitive regex-matching"
  87             print(m) > "/dev/stderr"
  88             exit 125
  89         }
  90         if (ci) IGNORECASE = 1
  91 
  92         for (i = 1; i < ARGC; i++) {
  93             args[++n] = ARGV[i]
  94             delete ARGV[i]
  95             if (erase) args[++n] = ""
  96         }
  97     }
  98 
  99     {
 100         # ignore cursor-movers and style-changers
 101         gsub(/\x1b\[[0-9;]*[A-Za-z]/, "")
 102 
 103         # ignore OSC sequences
 104         gsub(/\x1b\][^\x1b]+\x1b\\/, "")
 105 
 106         for (i = 1; i <= n; i += 2) gsub(args[i], args[i + 1])
 107         print
 108         if (flush) fflush()
 109     }
 110 ' "$@"