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}' when i write it to a bash file.then execute it,do a loop.like Code:
#!/bin/sh I so confuse...is there anyone give me a hint... thx |
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. |