LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   got a problem by fetching multi-line by random with awk (https://www.linuxquestions.org/questions/linux-newbie-8/got-a-problem-by-fetching-multi-line-by-random-with-awk-4175500998/)

xiutuo 04-08-2014 11:23 AM

got a problem by fetching multi-line by random with awk
 
write a bash shell to fetch multi-lines from a file by random with akw .

Code:

awk 'BEGIN{srand()}{b[rand()NR]=$0}END{for(x in b)print b[x]}' myfile | awk -v RS=''  '{gsub("\n"," "); print }' | awk '{print substr($0,1,250)}' | awk '{$NF="";print $0}'
on terminal commanline,everytime I execute it shows different result.

when i write it to a bash file.then execute it,do a loop.like
Code:

#!/bin/sh
    for (( i=1; i<6; i++)); do
awk 'BEGIN{srand()}{b[rand()NR]=$0}END{for(x in b)print b[x]}' myfile | awk -v RS=''  '{gsub("\n"," "); print }' | awk '{print substr($0,1,250)}' | awk '{$NF="";print $0}'
    done

do a loop,result each time is the same.

I so confuse...is there anyone give me a hint...

thx

grail 04-08-2014 12:44 PM

Well I am not surprised about being confused.

First off, is there any reason we need to use awk 4 times on the one line?? Surely they could all be compressed into 1.

Now as for your question:

1. I am curious what the point of placing NR at the end of rand() is supposed to achieve? rand() will return something like, 0.628009, so placing NR (ie first line will be 1) at the end simply gives you
0.6280091. Not sure I see the value??

2. As for not providing different answers when used in your loop, srand() uses the current time to create a random number, I would suggest that with a small file and such low iterations that all 5 loops
may be performed in less than a second. So possibly you are getting the same value ... just a guess by the way.

So my suggestion would be to rewrite the awks into 1 and then perhaps place a sleep in the loop so that the time moves forward prior to next call to awk.


All times are GMT -5. The time now is 04:27 AM.