Say you have file notifications.txt
. We need to count total number of lines, to determine range of random generator:
$ cat notifications.txt | wc -l
Lets write to variable:
$ LINES=$(cat notifications.txt | wc -l)
Now to generate number from 0
to $LINE
we will use RANDOM
variable.
$ echo $[ $RANDOM % LINES]
Lets write it to variable:
$ R_LINE=$(($RANDOM % LINES))
Now we only need to print this line number:
$ sed -n "${R_LINE}p" notifications.txt
About RANDOM:
RANDOM Each time this parameter is referenced, a random integer between
0 and 32767 is generated. The sequence of random numbers may be
initialized by assigning a value to RANDOM. If RANDOM is unset,
it loses its special properties, even if it is subsequently
reset.
Be sure your file have less then 32767 line numbers. See this if you need bigger random generator that works out of the box.
Example:
$ od -A n -t d -N 3 /dev/urandom | tr -d ' '