Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Subject: randomly select lines from a list

Original Message
Name: badger
Date: April 15, 2008 at 12:24:34 Pacific
Subject: randomly select lines from a list
OS: Mac OSX
CPU/Ram: N/A
Model/Manufacturer: mac
Comment:
Hi all,

I have a text file that has 142 lines each containing
several columns. I'm trying to write a script that will
randomly choose half of the lines, then write these to a
new text file. So, the result would be a file similar to the
original, but containing only 71 lines with none repeating.
Any advice/help would be greatly appreciated.

Nickles

b


Report Offensive Message For Removal

Response Number 1
Name: nails
Date: April 15, 2008 at 13:17:42 Pacific
Subject: randomly select lines from a list
Reply: (edit)
Take a look at this thread:

http://www.computing.net/answers/un...

It describes using the shell's RANDOM number generator.

It doesn't talk about repeating lines. You'll have to save the lines already used.


Report Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: April 16, 2008 at 03:33:01 Pacific
Subject: randomly select lines from a list
Reply: (edit)

awk 'BEGIN{srand()}
!/^$/{ a[c++]=$0}
END {
for ( i=1;i<=c ;i++ ) {
num=int(rand() * c)
if ( a[num] ) {
print a[num]
delete a[num]
d++
}
if ( d == c/2 ) break
}
}' file



Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: randomly select lines from a list

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software



Version Tracker Pro
Keep your software current and secure, effortlessly

Click Here for a Free Scan

Driver Agent
Automatically find the latest drivers for your computer.
Click Here for a Free Scan



The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC