💾 Archived View for tilde.club › ~filip › tech › script › versify › versify.sh.txt captured on 2023-04-26 at 14:00:28.
⬅️ Previous capture (2022-07-16)
-=-=-=-=-=-=-
#!/bin/bash # _ __ _ #__ _____ _ __ ___(_)/ _|_ _ ___| |__ #\ \ / / _ \ '__/ __| | |_| | | | / __| '_ \ # \ V / __/ | \__ \ | _| |_| |_\__ \ | | | # \_/ \___|_| |___/_|_| \__, (_)___/_| |_| # |___/ # LICENCE: PUBLIC DOMAIN function printhelp(){ echo "" echo "./versify.sh" echo "" echo "List books and print random series or verse:" echo " ./versify.sh <FILE> -l -- list books" echo " ./versify.sh <FILE> -s -- random series" echo " ./versify.sh <FILE> -v -- random verse" echo "" echo "Get up to 12 random words from:" echo " ./versify.sh <FILE> -o" echo "" echo "Print verses in a reference:" echo " ./versify.sh <FILE> <REF> [<REF> <REF> ...]" echo " Exampes of a reference:" echo " 1:2:1,3,5-10 -- 1st book, 2nd series, verses 1,3,5,6,7,8,9,10" echo " /key -- verses containing 'key'" echo " 1/key -- verses from the 1st book containing 'key'" echo " *:1:1 -- 1st series, 1st verse of all the books" echo " 1-2:1 -- 1st series of books 1 and 2" echo "" echo "Interactive mode:" echo " ./versify.sh <FILE> -i" echo "" echo "Help:" echo " ./versify.sh -h" echo "" } printhelp_im(){ echo "Interactive mode commands:" echo " l [<key>] -- list poems [containt <key> in title]" echo " r <key> -- search verses for <key>" echo " s -- random series" echo " v -- random verse" echo " o -- 10 random words" echo " h -- help" echo " q -- quit" } # NO FLAG OR HELP FLAG if [[ ! -f "$1" ]]; then if [[ "$1" = "-h" ]] || [[ $# -eq 0 ]]; then printhelp else echo "Wrong usage. Use -h for help." fi fi # INTERPRET THE COMMAND if [[ $# -ge 2 ]]; then # CHECK IF THE FILE IS VALID if [[ -f "$1" ]]; then fullfilelocation="$(readlink -f $1)" else echo "File "$1" does not exit." fi # CHECK THE FLAG if [[ "${2:0:1}" = "-" ]]; then case $2 in # LIST BOOKS -l) if [[ "$#" -gt 2 ]]; then # SEARCH BOOKS TITLES awk -F'\t' -v key="${*:3}" 'toupper($2)~toupper(key){if(!x[$1]++){printf "%02d\t%s\n",$1,$2}}' "$fullfilelocation" else # LIST ALL BOOKS awk -F'\t' '{if(!x[$1]++){printf "%02d\t%s\n",$1,$2}}' "$fullfilelocation" fi ;; # PRINT SERIES -s) if [[ "$#" -gt 2 ]]; then # PRINT A SPECIFIC SERIES awk -F'\t' -v num="$3" '$3==num{printf "%s\t%s\t%d\t%s\n",$2,$5,$7,$8;ok=1} $3!=num{if(ok){exit}}' "$fullfilelocation" else # PRINT A RANDOM SERIES n_max=$(tail -n1 "$fullfilelocation"|awk -F"\t" '{print $3}') n=$(echo $(($RANDOM%$n_max))) awk -F'\t' -v num="$n" '$3==num{printf "%s\t%s\t%d\t%s\n",$2,$5,$7,$8;ok=1} $3!=num{if(ok){exit}}' "$fullfilelocation" fi ;; # PRINT VERSE -v) if [[ $# -gt 2 ]]; then # PRINT SPECIFIC VERSE awk -F'\t' -v num="$3" '{if(NR==num){printf "%s\t%d\t%d\t%s\n",$2,$5,$7,$8};if(NR>num){exit}}' "$fullfilelocation" else # PRINT RANDOM VERSE n_max=$(tail -n1 "$fullfilelocation"|awk -F"\t" '{print $6}') n=$(echo $(($RANDOM%$n_max))) awk -F'\t' -v num="$n" '{if(NR==num){printf "%s\t%d\t%d\t%s\n",$2,$5,$7,$8};if(NR>num){exit}}' "$fullfilelocation" fi ;; # ORACLE -o) word_list=${fullfilelocation:0:${#fullfilelocation}-3}"word_list" if [[ -f "$word_list" ]]; then shuf -n10 "$word_list" else echo "No word list file. Generating..." touch "$word_list" gawk -F'\t' '{gsub(/^.*\t/,""); gsub(/0|1|2|3|4|5|6|&|8|9|\.|,|!|\?|"|#|“|„|;|\(|\)|”|\{|\}|ˮ|»|«|:|—|/,""); split($0,t," "); for(i=1;i<=length(t);i++){if(!seen[toupper(t[i])]++){print toupper(t[i])}}}' "$fullfilelocation" > "$word_list" echo "Done." shuf -n10 "$word_list" fi ;; # INTERACTIVE MODE -i) printhelp_im while [[ 1 ]]; do read -p "> " icmd num_key case $icmd in l) ./recite.sh "$fullfilelocation" -l "$num_key" ;; s) ./recite.sh "$fullfilelocation" -s "$num_key" ;; v) ./recite.sh "$fullfilelocation" -v "$num_key" ;; o) ./recite.sh "$fullfilelocation" -o ;; h) printhelp_im ;; r) ./recite.sh "$fullfilelocation" "$num_key" ;; q) break ;; *) echo "No such command. Use h for help." ;; esac done ;; *) echo "Wrong usage. Use -h for help." ;; esac else # SEARCH VERSES shift for x in "$@"; do awk -f versify.awk -v fullfilelocation=$fullfilelocation -v cmd="$x" $fullfilelocation done fi fi