feat(files): add _randomLineFromFile_

This commit is contained in:
Nathaniel Landau
2023-08-29 11:57:22 -04:00
parent 64c8658c8e
commit 69c1b491bb
4 changed files with 175 additions and 135 deletions

View File

@@ -749,6 +749,31 @@ _printFileBetween_() (
fi
)
_randomLineFromFile_() {
# DESC:
# Returns a random line from a file
# ARGS:
# $1 (Required) - Input file
# OUTS:
# Returns random line from file
# USAGE:
# _randomLineFromFile_ "file.txt"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _fileToRead="$1"
local _rnd
[ ! -f "${_fileToRead}" ] \
&& {
error "'${_fileToRead}' not found"
return 1
}
_rnd=$((1 + RANDOM % $(wc -l <"${_fileToRead}")))
sed -n "${_rnd}p" "${_fileToRead}"
}
_readFile_() {
# DESC:
# Prints each line of a file