1
echo -n "Hi <name>. How are you?" | tl -name Shane

This is similar to python f-strings

1
2
3
name = "Eric"
age = 74
print(f"Hello, {name}. You are {age}.")
Hello, Eric. You are 74.

In fact, I should make an f-strings-based utility.

Build the replace-substring script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
export TTY

# * This is great for templates
# cat $HOME/scripts/replace-substring | replace-substring -m "o" -r "$(echo "hi\nyo")"

pat=
replacement=
IGNORECASE=0
while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
    -m) {
        pat="$2"
        pat="$(p "$pat" | esc '\')"
        shift
        shift
    }
    ;;

    -r) {
        replacement="$2"
        shift
        shift
    }
    ;;

    -i) {
        IGNORECASE=1
        shift
    }
    ;;

    *) break;
esac; done

awk -v IGNORECASE=$IGNORECASE -v pat="$pat" -v r="$replacement" '{gsub(pat,r)}1'

Build the tl script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
export TTY

stdin_exists() {
    ! [ -t 0 ]
}

if stdin_exists; then
    fp="$(tf txt)"
else
    echo "requires stdin" 1>&2
    exit 1
fi

while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
    -[a-z]*) {
        varname="$(p "$opt" | mcut -d- -f2)"
        contents="$2"
        shift
        shift
        varname="$(p "$varname" | sed 's/\s\+//g')"                                        # no spaces
        varname="$(p "$varname" | sed -e 's/\(.*\)/\L\1/')"                                # lowercase
        varname="$(p "$varname" | fuzzify-regex -s)"                                       # let params match fields with spaces

        cat "$fp" | ptw replace-substring -i -m "<$varname>" -r "$contents" | sponge "$fp" # perform variable replacement
        cat "$fp" | sed 's/\(\b1 [a-zA-Z]\+\)s\b/\1/g' | sponge "$fp"                      # fix singular plural
        cat "$fp" | sed 's/""/"/g' | sponge "$fp"                                          # fix CSV double quote
    }
    ;;

    *) break;
esac; done

cat "$fp"

Example

1
echo -n "Hi <name>. How are you?" | tl -name Shane
Hi Shane. How are you?

Example of usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
IFS= read -r -d '' list <<HEREDOC
fp|functional-programming
hs|haskell
tc|torch|pytorch
dj|django
rl|reinforcement-learning
dl|deep-learning
nlp|natural-language-processing
tf|tensorflow
clj|clojure
sy|spacy
codegen|code-generation
rpl|rosie
ps|purs|purescript
ds|datascience|data-science
py|python
tu|turtle
pb|problog
mp|megaparsack
rkt|racket
js|javascript
gh|github
gr|google
ml|machine-learning
ts|typescript
pl|perl
HEREDOC

IFS= read -r -d '' template <<'HEREDOC'
    <options>) {
        lang=<lastoption>
    }
    ;;
HEREDOC

printf -- "%s\n" "$list" | awk1 | while IFS=$'\n' read -r line && test -n "$line"; do
    lastopt="$(printf -- "%s\n" "$line" | sed 's/.*|//')"
    printf -- "%s\n" "$template" | tl -options "$line" -lastoption "$lastopt"
done
fp|functional-programming) {
    lang=functional-programming
}
;;

hs|haskell) {
    lang=haskell
}
;;

tc|torch|pytorch) {
    lang=pytorch
}
;;

dj|django) {
    lang=django
}
;;

rl|reinforcement-learning) {
    lang=reinforcement-learning
}
;;

dl|deep-learning) {
    lang=deep-learning
}
;;

nlp|natural-language-processing) {
    lang=natural-language-processing
}
;;

tf|tensorflow) {
    lang=tensorflow
}
;;

clj|clojure) {
    lang=clojure
}
;;

sy|spacy) {
    lang=spacy
}
;;

codegen|code-generation) {
    lang=code-generation
}
;;

rpl|rosie) {
    lang=rosie
}
;;

ps|purs|purescript) {
    lang=purescript
}
;;

ds|datascience|data-science) {
    lang=data-science
}
;;

py|python) {
    lang=python
}
;;

tu|turtle) {
    lang=turtle
}
;;

pb|problog) {
    lang=problog
}
;;

mp|megaparsack) {
    lang=megaparsack
}
;;

rkt|racket) {
    lang=racket
}
;;

js|javascript) {
    lang=javascript
}
;;

gh|github) {
    lang=github
}
;;

gr|google) {
    lang=google
}
;;

ml|machine-learning) {
    lang=machine-learning
}
;;

ts|typescript) {
    lang=typescript
}
;;

pl|perl) {
    lang=perl
}
;;