![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| read and write from a file | rinku | Shell Programming and Scripting | 2 | 01-11-2008 01:22 AM |
| How to read and write directory or file contents in c++ ? | namrata5 | High Level Programming | 3 | 09-28-2007 03:58 PM |
| sed to read and write to very same file | 435 Gavea | Shell Programming and Scripting | 5 | 06-29-2006 11:04 PM |
| popening for read and write | szzz | High Level Programming | 1 | 11-18-2003 12:05 PM |
| read, write & STDOUT_FILENO.... | M3xican | High Level Programming | 2 | 07-17-2002 04:41 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
How to read and write a random row from a file?
Lets say I have a file abc.txt and it has about 35 million rows. I would like to take a sample of 100 random rows from that file for my testing purpose and write it to a file say test.txt.
How do I do this operation? Thanks, Sashank |
|
||||
|
Quote:
sed -n "$line p" filename >> $OUTOUT |
|
|||||
|
Quote:
I wonder which one uses more cycles as they both would have to parse and reparse the file... I suspect yours will be quicker |
|
||||
|
You can combine all of them into a single sed script, which will be a lot quicker.
Say, given a list of numbers in increasing order in file1, you can Code:
sed -e 's/$/p/' file1 | sed -n -f - bigfile >samples (Not all sed implementations understand "-f -" I have been stymied to learn; you need a temporary file then, obviously.) If you are a bit clever you can also make it quit after printing the last one, to avoid needlessly reading the big input file through to the end. Implementing that is left as an exercise for the astute reader. (-: Last edited by era; 05-13-2008 at 05:14 AM.. Reason: Oops, forgot sed -n |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|