bbl

Read, search and index the Bible on the command line -- Greek, Latin, KJV, Knox, RSV, and more
git clone git://git.wilsonrgheen.com/bbl
Log | Files | Refs | README | LICENSE

commit bedae1029efa757bb306781d2842f2f8a7f1075a
parent d8996e9c9966339fc79b42122fb7d2ae91c9e057
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date:   Wed,  9 Feb 2022 12:59:15 -0600

Added ability to get random verses.

Diffstat:
MREADME.md | 7+++++++
Maliasrc.sample | 1+
Mbbl.awk | 45++++++++++++++++++++++++++++++++++-----------
Mbbl.sh | 35++++++++++++++++++++++++++++-------
4 files changed, 70 insertions(+), 18 deletions(-)

diff --git a/README.md b/README.md @@ -47,6 +47,13 @@ usage: bbl [flags] [bible] [reference...] All verses in a book that match a pattern <Book>:<Chapter>/<Search> All verses in a chapter of a book that match a pattern + + @ <Number-of-Verses>?" + Random verse or assortment of verses from any book/chapter" + <Book> @ <Number-of-Verses>?" + Random verse or assortment of verses from any chapter in a given book" + <Book>:<Chapter> @ <Number-of-Verses>?" + Random verse or assortment of verses from the given book:chapter" ``` ## Note diff --git a/aliasrc.sample b/aliasrc.sample @@ -5,4 +5,5 @@ alias \ njb="bbl --jerusalem" \ knx="bbl --knox" \ kjv="bbl --kjv" \ + rsv="bbl --rsv" \ vul="bbl --vulgate" diff --git a/bbl.awk b/bbl.awk @@ -8,6 +8,7 @@ BEGIN { FS = "\t" MAX_WIDTH = 80 + NO_LINE_WRAP = ENVIRON["KJV_NOLINEWRAP"] != "" && ENVIRON["KJV_NOLINEWRAP"] != "0" if (ENVIRON["KJV_MAX_WIDTH"] ~ /^[0-9]+$/) { if (int(ENVIRON["KJV_MAX_WIDTH"]) < MAX_WIDTH) { MAX_WIDTH = int(ENVIRON["KJV_MAX_WIDTH"]) @@ -20,6 +21,10 @@ BEGIN { } } +cmd == "clean" { + processline() +} + cmd == "list" { if (!($2 in seen_books)) { printf("%s (%s)\n", $1, $2) @@ -39,21 +44,22 @@ function parseref(ref, arr) { # 7. /<search> # 8. <book>/search # 9. <book>:?<chapter>/search + #10. @ <number of verses>? + #11. <book> @ <number of verses>? + #12. <book>:?<chapter> @ <number of verses>? if (match(ref, "^[1-9]?[a-zA-Z ]+")) { - # 1, 2, 3, 3a, 3b, 4, 5, 6, 8, 9 + # 1, 2, 3, 3a, 3b, 4, 5, 6, 8, 9, 11, 12 arr["book"] = substr(ref, 1, RLENGTH) ref = substr(ref, RLENGTH + 1) } else if (match(ref, "^/")) { # 7 arr["search"] = substr(ref, 2) return "search" - } else { - return "unknown" } if (match(ref, "^:?[1-9]+[0-9]*")) { - # 2, 3, 3a, 3b, 4, 5, 6, 9 + # 2, 3, 3a, 3b, 4, 5, 6, 9, 12 if (sub("^:", "", ref)) { arr["chapter"] = int(substr(ref, 1, RLENGTH - 1)) ref = substr(ref, RLENGTH) @@ -68,8 +74,6 @@ function parseref(ref, arr) { } else if (ref == "") { # 1 return "exact" - } else { - return "unknown" } if (match(ref, "^:[1-9]+[0-9]*")) { @@ -87,6 +91,16 @@ function parseref(ref, arr) { } else if (ref == "") { # 2 return "exact" + } else if (match(ref, "^ *@ *")) { + # 10, 11, 12 + ref = substr(ref, RLENGTH + 1) + if (match(ref, "^[1-9][0-9]*")) { + arr["numberOfVerses"] = int(ref) + } else { + arr["numberOfVerses"] = 1 + } + NO_LINE_WRAP = 1 + return "random" } else { return "unknown" } @@ -177,7 +191,7 @@ function bookmatches(book, bookabbr, query) { } function printverse(verse, word_count, characters_printed) { - if (ENVIRON["KJV_NOLINEWRAP"] != "" && ENVIRON["KJV_NOLINEWRAP"] != "0") { + if (NO_LINE_WRAP) { printf("%s\n", verse) return } @@ -205,9 +219,9 @@ function printverse(verse, word_count, characters_printed) { function processline() { if (last_book_printed != $2) { if(cross_ref) { - printf("\n") + print("") } else { - print $1 + print($1) } last_book_printed = $2 } @@ -223,6 +237,11 @@ cmd == "ref" && mode == "exact" && bookmatches($1, $2, p["book"]) && (p["chapter processline() } +cmd == "ref" && mode == "random" && (p["book"] == "" || bookmatches($1, $2, p["book"])) && (p["chapter"] == "" || $4 == p["chapter"]) { + print + outputted_records++ +} + cmd == "ref" && mode == "exact_set" && bookmatches($1, $2, p["book"]) && (((p["chapter"] == "" || $4 == p["chapter"]) && p["verse", $5]) || p["chapter:verse", $4 ":" $5]) { processline() } @@ -240,7 +259,11 @@ cmd == "ref" && mode == "search" && (p["book"] == "" || bookmatches($1, $2, p["b } END { - if (cmd == "ref" && outputted_records == 0) { - print "Unknown reference: " ref + if (cmd == "ref") { + if (outputted_records == 0) { + print "Unknown reference: " ref + } else if (mode == "random") { + printf("~~~RANDOMS: %d\n", p["numberOfVerses"]) + } } } diff --git a/bbl.sh b/bbl.sh @@ -31,8 +31,8 @@ show_help() { echo " -g, --greek Greek Bible (Septuagint + SBL NT)" echo " -j, --jerusalem New Jerusalem Bible" echo " -k, --kjv King James Bible" - echo " -r, --rsv Revised Standard Version: Catholic Edition" echo " -n, --knox Knox Bible" + echo " -r, --rsv Revised Standard Version: Catholic Edition" echo " -v, --vulgate Clementine Vulgate" echo echo " Reference types:" @@ -57,6 +57,13 @@ show_help() { echo " All verses in a book that match a pattern" echo " <Book>:<Chapter>/<Search>" echo " All verses in a chapter of a book that match a pattern" + echo + echo " @ <Number-of-Verses>?" + echo " Random verse or assortment of verses from any book/chapter" + echo " <Book> @ <Number-of-Verses>?" + echo " Random verse or assortment of verses from any chapter in a given book" + echo " <Book>:<Chapter> @ <Number-of-Verses>?" + echo " Random verse or assortment of verses from the given book:chapter" exit 2 } @@ -143,19 +150,33 @@ if [ $# -eq 0 ]; then exit 0 fi -crossRef=0 -i=1 +i=0 myTempDir=$(mktemp -d "${TMPDIR:-/tmp/}$(basename $0).XXXXXXXXXXXX") for version in $BIBLE; do - get_data ${version}.tsv 2>/dev/null | awk -v cmd=ref -v ref="$*" -v cross_ref="$crossRef" "$(get_data bbl.awk)" 2>/dev/null > "${myTempDir}/${i}-${version}.txt" + get_data ${version}.tsv 2>/dev/null | awk -v cmd=ref -v ref="$*" -v cross_ref="${i}" "$(get_data bbl.awk)" 2>/dev/null > "${myTempDir}/${i}-${version}.txt" i=$((i + 1)) - crossRef=1 done +oldDir="$(pwd)" cd "${myTempDir}" -if [ ${crossRef} ]; then +if [ ${i} -gt 1 ]; then paste $(ls) -d "@" | column -t -s "@" -o " " | sed '/^[a-zA-Z]/s/^/\t/;1s/^ *//;' | ${PAGER} else - ${PAGER} "${myTempDir}/$(ls)" + myFile="$(ls)" + fullPath="${myTempDir}/${myFile}" + cd "${oldDir}" + lastLine="$(tail -n1 ${fullPath})" + + echo $lastLine | grep -q '~~~RANDOMS:' + if [ $? -eq 0 ]; then + numberOfVerses=$(echo "${lastLine}" | grep -o '[0-9]*') + linesInFile=$(($(wc -l "$fullPath" | awk '{print $1}') - 1)) + sedCmd=$(shuf -i 1-$linesInFile -n $numberOfVerses | sort -n | tr '\n' ' ' | sed 's/ /p;/g' | sed 's/..$/{p;q}/') + sed -n "$sedCmd" "$fullPath" > "${myTempDir}/randomVerses" + awk -v cmd=clean "$(get_data bbl.awk)" "${myTempDir}/randomVerses" 2>/dev/null > "${fullPath}" + fi + + ${PAGER} "${fullPath}" + fi rm -rf "${myTempDir}"