The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
S-231: Adobe Form Designer and Form Client Vulnerabilities iBot Security Advisories (RSS) 0 03-27-2008 06:10 AM
How to parse through a file and based on condition form another output file sivasu.india UNIX for Advanced & Expert Users 6 02-28-2008 12:59 AM
Getting a random file davidY Shell Programming and Scripting 6 01-01-2007 01:03 AM
tar form a file ppass UNIX for Dummies Questions & Answers 2 03-29-2005 08:52 PM
Changing Unix form to Microsoft Word form to be able to email it to someone. Cheraunm UNIX for Advanced & Expert Users 8 05-24-2002 12:58 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-10-2008
Registered User
 

Join Date: Mar 2008
Posts: 18
Stumble this Post!
Random lines selection form a file.

Quote:
I have a data file with 1000 records, I would like to select 100 lines/records randomly from the data file(data.dat) to other file data_extract.dat

The contents of the data.dat file as below:
Code:
>cat data.dat
0001 Robbert
0002 Nick
0003 Mark
…….
1000 Jarek
Quote:
Do you have any suggestion for this with korn shell script?

Regards.
McLan
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-10-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,426
Stumble this Post!
Prepend a random number to each line, sort the file, take the first few lines, and remove the leading random number.

Code:
 awk 'BEGIN {srand()} {printf "%05.0f %s \n",rand()*99999, $0; }' datafile | sort -n | head -100 | sed 's/^[0-9]* //'
Reply With Quote
  #3 (permalink)  
Old 06-10-2008
Registered User
 

Join Date: Mar 2008
Posts: 18
Stumble this Post!
I afraid, if this is going to generate any duplicate random numbers in range from 1 to 100? If yes then how can you avoid that?
If this generates the duplicate numbers then the same record will be picked up more than once.

Cheers,
McLan
Reply With Quote
  #4 (permalink)  
Old 06-10-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,426
Stumble this Post!
Why don't you try it? The range of rand()*99999 is not 1-100, duplicate random numbers are possible, duplicate lines selected from your file are not possible.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0