You are not logged in.
so I've written this script in bash and it works ok on arch. among other things I need to choose a random line from a text file. I used shuf -n1, that does just what I need.
the problem is: when I try to run this script on redhat or other linuxes that used older coreutils shuf isn't available. Also sort -R isn't there.
So what's the simplest portable way to do this?
[I'm not sure that "Newbie corner" is the right place to post this, if not please move it where it belongs. I didn't find a better place]
Offline
man bash | fgrep -A1 RANDOM
Offline
ugly hack, probably a better way to do this.
FILE='filename.txt'
let "n=${RANDOM}%$(wc -l < $FILE)"
head -n $n $FILE | tail -1
Last edited by rson451 (2010-01-21 20:33:26)
archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson
Offline
Edit:
Never mind, must start reading:D (sort -R not available)
Last edited by stefos (2010-01-21 16:48:18)
Offline
This also seems to work:
awk -v LINES=$(wc -l $myfile) 'BEGIN {srand(); line=1+int(rand()*LINES) } --line==0 { print; quit }; { next }' < $myfile
Offline
$(cat $FILE | wc -l)
Useless cat, wc takes arguments, use wc -l $FILE
Offline
rson451 wrote:$(cat $FILE | wc -l)
Useless cat, wc takes arguments, use wc -l $FILE
The output won't be the same. But you could use wc -l < $FILE
Offline
The output won't be the same. But you could use wc -l < $FILE
Correct. Thanks for the redirect thing though, I knew I was missing something and just couldn't grab it. I had tried $(< $FILE | wc -l) but that fails as well.
Editing my first post to reflect this.
Last edited by rson451 (2010-01-21 20:33:09)
archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson
Offline
thanks for all suggestions.
I'll probably use rson451's solution because I can understand it. awk is too hardcore for me.
Offline
Yeah, I agree much cleaner to do it in shell alone, only problem is your distribution won't be flat unless your number of lines is a power of 2. If the number of lines is small, it won't be very far from flat. But to make the problem vivid, suppose you have 21884 lines. Then the first half of them are going to be chosen via the Bash method given twice as often as the second half. If you have 32766 lines, the first line will will be chosen twice as often as all the rest. You can work around that in the shell alone, but it will be more complex. Depending on your intended use, you might just choose to ignore this.
Offline