I was trying to assign below command(which choose the random line from file) to a variable, but not working.
givinv@87-109:~$ head -$((${RANDOM} % `wc -l < file` + 1)) file | tail -1
cower
givinv@87-109:~$
Below the the error I am getting while trying to assign it to a variable.
givinv@87-109:~$ VARIA=`head -$((${RANDOM} % `wc -l < file` + 1)) file | tail -1`
bash: command substitution: line 1: unexpected EOF while looking for matching `)'
bash: command substitution: line 2: syntax error: unexpected end of file
bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: ` + 1)) file | tail -1'
-l: command not found
givinv@87-109:~$
I even tried same with for loop and not working::
givinv@87-109:~$ for i in `head -$((${RANDOM} % `wc -l < file` + 1)) file | tail -1`;do echo $i ;done
bash: syntax error near unexpected token `<'
givinv@87-109:~$
shuf
– Stéphane Chazelas Nov 28 '16 at 11:22$RANDOM
's maximum value inbash
is 32767, so you would not be able to reach lines greater than that. Also note that it would not give you a uniform random distribution unless the number of lines in the file is a divisor of 32768 (is a power of 2). – Stéphane Chazelas Nov 28 '16 at 11:26