Some days ago I came with an idea of getting a random phrase from a file were I am used to save phrases with new English words I am learning. The first draft is this:
#!/usr/bin/env bash
# Filename: getphrase.sh
# Created: 2019-05-24 07:33
# Last Change: May 24 2019 08:23
# Author: Sérgio Araújo - voyeg3r at gmail
# PRINT A RANDOM PARAGRAPH FROM A GIVEN FILE
# I have a file called phrases were I collect phrases in English
# that I am learning and I had the crazy idea of getting on random phrase
# This script will get a random phrase (the criteria is: paragraphs separated by blank lines)
# target file
file=~/.dotfiles/nvim/wiki/phrases.md
# count registers (number of phrases)
# RS -> Register Separator == "" (which means blank lines)
# FS -> Field Separator == "\n" (which means line breaks)
# NR -> prints the register number for all registers
# once we want the number of registers we are getting the last one
COUNT=$(awk 'BEGIN {RS="";FS="\n"} {print NR}' $file | tail -1)
# print a random number: shuf -i 1-10 -n 1
# from the third phrase (the first two are explanatory of the file)
# the last line contains tags, nor a real phrase so I have to get
# from the third paragraph to the end minus one
# var=10 ; echo $((var - 2))
RANDPHRASE=$(shuf -i 3-$((COUNT -1)) -n 1)
clear
echo "phrase number: $RANDPHRASE"
echo
# if the NR (number of register) is equal to "number"
awk -v number=$RANDPHRASE 'BEGIN {RS="";FS="\n"} NR==number' $file
echo
Top comments (0)