File: match.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 # match [options...] [regular expressions...] 27 # 28 # Only keep lines which match any of the extended-mode regular expressions 29 # given. When not given any regex, match non-empty lines by default. 30 # 31 # Input lines always come from the standard input. 32 # 33 # The options are, available both in single and double-dash versions 34 # 35 # -h, -help show this help message 36 # -i, -ins match regexes case-insensitively 37 # -l, -links add a regex to match HTTP/HTTPS links case-insensitively 38 39 40 case "$1" in 41 -h|--h|-help|--help) 42 awk '/^# +match /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 43 exit 0 44 ;; 45 esac 46 47 links=0 48 avoid=0 49 buffered=0 50 case_ins=0 51 52 while [ $# -gt 0 ]; do 53 case "$1" in 54 -b|--b|-buffered|--buffered) 55 buffered=1 56 shift 57 continue 58 ;; 59 60 -i|--i|-ins|--ins|-insensitive|--insensitive) 61 case_ins=1 62 shift 63 continue 64 ;; 65 66 -iv|-vi) 67 case_ins=1 68 avoid=1 69 shift 70 continue 71 ;; 72 73 -l|--l|-links|--links) 74 links=1 75 shift 76 continue 77 ;; 78 79 -v|--v|-inv|--inv|-neg|--neg) 80 avoid=1 81 shift 82 continue 83 ;; 84 esac 85 86 break 87 done 88 89 [ "$1" = '--' ] && shift 90 91 flush=0 92 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then 93 flush=1 94 fi 95 96 re='[Hh][Tt][Tt][Pp][Ss]?://[A-Za-z0-9+_.:%-]+(/[A-Za-z0-9+_.%/,#?&=-]*)*' 97 98 awk -v flush="${flush}" -v ci="${case_ins}" -v add_links="${links}" \ 99 -v links="${re}" -v avoid="${avoid}" ' 100 BEGIN { 101 if (ci && IGNORECASE == "") { 102 msg = "this variant of AWK lacks case-insensitive regex-matching" 103 print(msg) > "/dev/stderr" 104 exit 125 105 } 106 if (ci) IGNORECASE = 1 107 108 for (i = 1; i < ARGC; i++) { 109 re[i] = ARGV[i] 110 delete ARGV[i] 111 } 112 113 n = ARGC - 1 114 if (add_links) re[++n] = links 115 if (n == 0) re[++n] = "[^\\r]" 116 } 117 118 { 119 plain = $0 120 gsub(/\x1b\[([0-9]{4,}[A-Za-z])/, "", plain) 121 122 for (i = 1; i <= n; i++) { 123 if (plain ~ re[i]) { 124 if (avoid) next 125 print 126 if (flush) fflush() 127 got++ 128 next 129 } 130 } 131 132 if (avoid) { print; if (flush) fflush() } 133 } 134 135 END { exit(got == 0) } 136 ' "$@"