LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-08-2014, 12:23 PM   #1
xiutuo
Member
 
Registered: Mar 2008
Posts: 52

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 .

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

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

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


Reply


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 10:00 AM
[SOLVED] Recognize multi-line pattern and delete certain lines (awk/sed?) legato Programming 4 10-31-2012 05:02 PM
[SOLVED] grep/sed/awk multi line array earthgecko Programming 13 10-08-2012 02: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 12:38 PM
[SOLVED] split multi line record into multiple files with awk pcock Programming 10 11-12-2009 05:12 AM


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

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