Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-08-2014, 11:23 AM   #1
Registered: Mar 2008
Posts: 55

Rep: Reputation: 15
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 .

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
    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}'
do a loop,result each time is the same.

I so there anyone give me a hint...


Last edited by colucix; 04-08-2014 at 12:14 PM. Reason: Changed PHP code tags to plain CODE tags to improve readability
Old 04-08-2014, 12:44 PM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk/se: splitting a multi-line input at a regular expression and rearranging sarenace Programming 9 11-27-2012 09:00 AM
[SOLVED] Recognize multi-line pattern and delete certain lines (awk/sed?) legato Programming 4 10-31-2012 04:02 PM
[SOLVED] grep/sed/awk multi line array earthgecko Programming 13 10-08-2012 01:06 PM
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line cristalp Programming 3 11-23-2011 11:38 AM
[SOLVED] split multi line record into multiple files with awk pcock Programming 10 11-12-2009 04:12 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration