PERL: Extract random record which has 4 lines each

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: Extract random record which has 4 lines each
# 1  
Old 10-13-2009
PERL: Extract random record which has 4 lines each


I have a data file with millions of record (N). Each record was saved in 4 lines. So there are total of NX4 lines in the data file.

For Example:


I would like to write a PERL script to extract 1000 random records , WITHOUT repeating/replacement. So, there is total of 4000 lines in output file.

Could you help me this ?

# 2  
Old 10-13-2009
what have you done to accomplish your desire to write a perl script for this? What problems are you having?
# 3  
Old 10-13-2009
I have code below to randomly select number of records (1 line for each record only) from file.
I'm thinking to modify this code in a way like, if the selected random number is 6, which means record 6 is picked, then it will retrieve lines from (5*4)+1 (which is 21) to line 24.

This is my first time writting perl script. Please help.


die "Usage: $0 <N>, where N is the number of lines to pick\n"
if @ARGV<1;
$N = shift@ARGV;

if (@pick < $N) {
push @pick,$_;
} else {
rand($.)<=$N and $pick[rand(@pick)]=$_;

print @pick;
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

2. Shell Programming and Scripting

Extract record from file based on section.

input file output file (1 Reply)
Discussion started by: lathigara
1 Replies

3. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. Shell Programming and Scripting

PERL: extract lines between two patterns

Hello Perl-experts, I am new to perl and need help to solve a problem. I have a table in below format. <Text A> <Pattern1> A Value B Value C Value D Value <Pattern2> <Text B> This table is in file1. I want to extract lines between Pattern1 and Pattern2 and write it into file2.... (11 Replies)
Discussion started by: mnithink
11 Replies

5. Shell Programming and Scripting

Script to extract particular record

Hi, I have a large file with huge number of records which are of following pattern: TYPE1 { originNodeType : "IVR" originHostName : "AAIVR" originTransactionID : "01310559" originTimeStamp : "20110620192440+0530" hostName : "hhhh" voucher : '0'D rProfileID : "ZZZZ" Before { Flags :... (1 Reply)
Discussion started by: madhukar1anand
1 Replies

6. Shell Programming and Scripting

Unix command to extract a record from a table

Suppose there is a table like the following...I just wanted to know if there is any command using which we can get the record/name of the person who joined before 2005.. Sl Name des y.o.joining 1 Ram Engineer 2001 2 Hari Doctor 2004 3 David Plumber 2005 4 Rahim painter 2007 5 gurmeet... (1 Reply)
Discussion started by: satyajit007
1 Replies

7. Shell Programming and Scripting

How to extract first and last line of different record from a file

Hi all I want to inquire that is there any unix command that can help me while extracting first and last line in a file ( TEST.dat) for example in the below record i want to extract the line that are in BOLD text or in other words i want to extract line no 1,3,4 and 7 aa 1 2 3 aa 2 3 4... (5 Replies)
Discussion started by: Bungash125
5 Replies

8. UNIX for Dummies Questions & Answers

Using sed to extract Nth record?

I have a comma-separated record and I'd like to use sed to pull the Nth record from it. It seems like it'd need to be something like this: sed -n 's/'"\,$1\,"'/&/p' Am I close? (3 Replies)
Discussion started by: doubleminus
3 Replies

9. UNIX for Dummies Questions & Answers

How to extract duplicate records with associated header record

All, I have a task to search through several hundred files and extract duplicate detail records and keep them grouped with their header record. If no duplicate detail record exists, don't pull the header. For example, an input file could look like this: input.txt HA D1 D2 D2 D3 D4 D4... (17 Replies)
Discussion started by: run_eim
17 Replies

10. Shell Programming and Scripting

how to extract last line in record

Hi all!! After experiencing great helpfulness the last time I posted a problem at this site, I once again turn to the forum for expert help. The problem: I have a file.dat containing x, y, and z coordinates, like: x y z 1 1 4 1 2 3 1 3 9 2 1 7 2 2 2 2 3 8 3 1 ... (7 Replies)
Discussion started by: bjorb
7 Replies
Login or Register to Ask a Question