# tags: urlencode, url, search, lynx, alias, shell, awk, code # # awk script urlencodes output - ASCII chars only, # doesn't deal with multibyte UTF-8 chars # Michael Sanders 2023 # https://busybox.neocities.org/notes/urlencode.txt # # handy shell function & alias... # # search() { # query=$(echo "$*" | awk -f urlencode.txt) # lynx "https://lite.duckduckgo.com/lite/?q=$query" # } # # alias ?=search # # usage example: ? what is awk { print urlencode($0) } BEGIN { FS = OFS = "" hex = "0123456789ABCDEF" asc = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" } function urlencode(str, x, y, c, encoded) { encoded = "" y = length(str) for (x = 1; x <= y; x++) { c = substr(str, x, 1) if (c ~ /[0-9A-Za-z]/) encoded = encoded c # alphanumeric chars are not encoded else encoded = encoded "%" hexify(c) # other chars are percent-encoded } return encoded } function hexify(c, pos, hib, lob) { pos = index(asc, c) if (pos == 0) return "00" # handle non-printable chars (like \n, which shouldn't appear in URL) hib = int((pos + 31) / 16) lob = int((pos + 31) % 16) return substr(hex, hib + 1, 1) substr(hex, lob + 1, 1) } # eof