SH


Print a hex array with #Bash

seq 0 255 | awk '{printf "%02X ", $1}' | xargs -n16


Shuffle and rename the files in a directory of images:
$ echo '#!/bin/bash' > files; chmod 744 files; ls -1 | shuf | nl -i1 -s' ' -nrz -w5 | awk '{print "mv " $2 " " $1 ".jpg"}' >> files ; ./files


Calculate #Easter for a range of years

$ seq 2026 2046 | xargs -i ncal -e {}

04/05/2026
03/28/2027
04/16/2028
04/01/2029
04/21/2030
04/13/2031
03/28/2032
04/17/2033
04/09/2034
03/25/2035
04/13/2036
04/05/2037
04/25/2038
04/10/2039
04/01/2040
04/21/2041
04/06/2042
03/29/2043
04/17/2044
04/09/2045
03/25/2046


Perl one-liner to highlight consecutive duplicated words:

perl -pe 's/\b(\w+)\s+\1\b/\e[31m$&\e[0m/gi' gettysburg.txt




Remove empty directories under the working directory:

find . -empty -exec rm -rf {} \;




Sort the Holy Bible by verse length with Perl

perl -e 'print sort {length $b length $a} ' kjv.txt

NEH.10:16 – Adonijah, Bigvai, Adin,
NEH.10:18 – Hodijah, Hashum, Bezai,
NEH.10:27 – Malluch, Harim, Baanah.
NEH.12:5 – Miamin, Maadiah, Bilgah,
JOH.6:48 – I am that bread of life.
1CH.1:24 – Shem, Arphaxad, Shelah,
NEH.10:13 – Hodijah, Bani, Beninu.
NEH.10:17 – Ater, Hizkijah, Azzur,
NEH.11:33 – Hazor, Ramah, Gittaim,
NEH.12:4 – Iddo, Ginnetho, Abijah,
JOB.3:2 – And Job spake, and said,
EXO.20:15 – Thou shalt not steal.
1TH.5:19 – Quench not the Spirit.
1TH.5:25 – Brethren, pray for us.
EXO.20:13 – Thou shalt not kill.
NEH.10:15 – Bunni, Azgad, Bebai,
LUK.17:32 – Remember Lot’s wife.
1TH.5:17 – Pray without ceasing.
DEU.5:17 – Thou shalt not kill.
1CH.1:26 – Serug, Nahor, Terah,
1CH.1:1 – Adam, Sheth, Enosh,
1CH.1:25 – Eber, Peleg, Reu,
1TH.5:16 – Rejoice evermore.
JOH.11:35 – Jesus wept.


#! /bin/bash
D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
echo $((10#${D2B[$1]}))






Posted in Uncategorized | Leave a comment

Cmdlog

List bash commands by frequency

#!/usr/bin/env python3
import os
history_file = os.path.expanduser("~/.bash_history")
counts = {}
with open(history_file, "r", errors="ignore") as f:
    for line in f:
        line = line.strip()
        if not line or line.startswith("#"):
            continue
        cmdline = line.split("|", 1)[0].strip()
        parts = cmdline.split()
        if not parts:
            continue
        cmd = parts[1] if parts[0] == "sudo" and len(parts) > 1 else parts[0]

        counts[cmd] = counts.get(cmd, 0) + 1
sorted_cmds = sorted(counts.items(), key=lambda x: x[1], reverse=True)
for cmd, n in sorted_cmds:
    print(f"{n} {cmd:20}")

Posted in Uncategorized | Leave a comment

Rtf2txt

#!/usr/bin/env python3
import sys
import re
import codecs
SKIP_DESTINATIONS = {
    "fonttbl", "colortbl", "stylesheet", "info", "pict",
    "object", "objdata", "header", "headerl", "headerr",
    "footer", "footerl", "footerr", "filetbl", "listtable",
    "listoverridetable", "generator", "xmlnstbl", "datastore",
    "themedata", "colorschememapping", "latentstyles",
    "rsidtbl", "revtbl", "annotation", "atnauthor",
    "atnid", "atnref", "atntime", "aftncn", "aftnsep",
    "aftnsepc", "ftncn", "ftnsep", "ftnsepc",
    "object", "nonshppict", "shppict", "do", "formfield",
}
SPECIAL_CHARS = {
    "\\": "\\",
    "{": "{",
    "}": "}",
    "~": "\u00a0",      
    "_": "\u2011",       
    "-": "",            
}

SPECIAL_WORDS = {
    "emdash": "\u2014",
    "endash": "\u2013",
    "emspace": "\u2003",
    "enspace": "\u2002",
    "qmspace": "\u2005",
    "bullet": "\u2022",
    "lquote": "\u2018",
    "rquote": "\u2019",
    "ldblquote": "\u201c",
    "rdblquote": "\u201d",
    "zwj": "\u200d",
    "zwnj": "\u200c",
}


def convert(data):
    text = data.decode("latin-1")
    state = {
        "skip": False,
        "uc": 1,
        "codepage": "cp1252",
        "ignorable": False,
    }
    stack = []

    output = []
    i = 0
    n = len(text)

    def emit(s):
        if not state["skip"]:
            output.append(s)

    def decode_byte(value):
        try:
            return bytes([value]).decode(
                state["codepage"], errors="replace"
            )
        except LookupError:
            return bytes([value]).decode("cp1252", errors="replace")

    def decode_raw(s):
        """Decode raw RTF text bytes using the current code page."""
        if state["skip"]:
            return

        raw = bytes(ord(c) for c in s)
        try:
            output.append(
                raw.decode(state["codepage"], errors="replace")
            )
        except LookupError:
            output.append(raw.decode("cp1252", errors="replace"))

    while i < n:
        c = text[i]

        if c == "{":
            stack.append(state.copy())
            state = state.copy()
            state["ignorable"] = False
            i += 1
            continue

        if c == "}":
            if stack:
                state = stack.pop()
            i += 1
            continue

        if c != "\\":
            if c == "\r" or c == "\n":
                i += 1
                continue

            if c == "\t":
                emit("\t")
                i += 1
                continue

            decode_raw(c)
            i += 1
            continue

        i += 1

        if i >= n:
            break

        c = text[i]

        if c == "'":
            if i + 2 < n:
                try:
                    value = int(text[i + 1:i + 3], 16)
                    emit(decode_byte(value))
                    i += 3
                    continue
                except ValueError:
                    pass

            i += 1
            continue

        if not c.isalpha():
            i += 1

            if c == "*":
                state["ignorable"] = True
                continue

            if c in SPECIAL_CHARS:
                emit(SPECIAL_CHARS[c])
                continue

            if c == "~":
                emit("\u00a0")
                continue

            if c == "\n" or c == "\r":
                continue

            if c == ":":
                continue

            if c == "|":
                emit("|")
                continue

            if c == "\\":
                emit("\\")
                continue

            continue

        start = i
        while i < n and text[i].isalpha():
            i += 1

        word = text[start:i]

        sign = 1
        if i < n and text[i] == "-":
            sign = -1
            i += 1

        num_start = i
        while i < n and text[i].isdigit():
            i += 1

        arg = None
        if i > num_start:
            arg = sign * int(text[num_start:i])

        if i < n and text[i] == " ":
            i += 1

        if state["ignorable"]:
            state["ignorable"] = False
            state["skip"] = True
            continue

        if word in SKIP_DESTINATIONS:
            state["skip"] = True
            continue

        if word == "ansicpg" and arg is not None:
            state["codepage"] = f"cp{arg}"
            continue

        if word == "uc" and arg is not None:
            state["uc"] = max(0, arg)
            continue

        if word == "u" and arg is not None:
            value = arg if arg >= 0 else arg + 65536
            emit(chr(value))

            fallback = state["uc"]
            while fallback > 0 and i < n:
                if text[i] == "\\":
                    if i + 1 < n and text[i + 1] == "'":
                        i = min(i + 4, n)
                    elif i + 1 < n and not text[i + 1].isalpha():
                        i += 2
                    else:
                        break
                elif text[i] in "{}":
                    break
                else:
                    i += 1

                fallback -= 1

            continue

        if word in ("par", "row"):
            emit("\n")
            continue

        if word == "line":
            emit("\n")
            continue

        if word == "tab":
            emit("\t")
            continue

        if word == "page":
            emit("\n")
            continue

        if word == "sect":
            emit("\n")
            continue

        if word in SPECIAL_WORDS:
            emit(SPECIAL_WORDS[word])
            continue

        if word == "emdash":
            emit("\u2014")
            continue

        if word == "enspace":
            emit("\u2002")
            continue

        if word == "ansi":
            state["codepage"] = "cp1252"
            continue

        if word == "mac":
            state["codepage"] = "mac_roman"
            continue

        if word == "pc":
            state["codepage"] = "cp437"
            continue

        if word == "pca":
            state["codepage"] = "cp850"
            continue

    result = "".join(output)
    result = result.replace("\r\n", "\n").replace("\r", "\n")

    result = re.sub(r"[ \t]+\n", "\n", result)

    result = re.sub(r"\n{4,}", "\n\n\n", result)

    return result


def main():
    data = sys.stdin.buffer.read()
    result = convert(data)
    sys.stdout.write(result)


if __name__ == "__main__":
    main()
#!/bin/bash
find . -type f -iname '*.rtf' -print0 |
while IFS= read -r -d  file; do
    rtf2txt < "$file" > "${file%.*}.txt"
done
Posted in Uncategorized | Leave a comment

MSDOS



Skyglobe for MSDOS, still the best astronomy software 35 years later…



Before we wasted time on Windows solitaire we wasted time on MSDOS solitaire.







Posted in Uncategorized | Leave a comment

Calculus

Posted in Uncategorized | Leave a comment