DEV Community

Sérgio Araújo
Sérgio Araújo

Posted on

3 2

Print a Random Phrase With Shell Script

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
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay