Sponsored Content
Top Forums Shell Programming and Scripting PERL: Extract random record which has 4 lines each Post 302361552 by phoeberunner on Tuesday 13th of October 2009 01:35:18 PM
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.

#!/usr/bin/perl

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

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

print @pick;
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

Extract record from file based on section.

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

10. 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
ReadBackwards(3pm)					User Contributed Perl Documentation					ReadBackwards(3pm)

NAME
File::ReadBackwards.pm -- Read a file backwards by lines. SYNOPSIS
use File::ReadBackwards ; # Object interface $bw = File::ReadBackwards->new( 'log_file' ) or die "can't read 'log_file' $!" ; while( defined( $log_line = $bw->readline ) ) { print $log_line ; } # ... or the alternative way of reading until ( $bw->eof ) { print $bw->readline ; } # Tied Handle Interface tie *BW, 'File::ReadBackwards', 'log_file' or die "can't read 'log_file' $!" ; while( <BW> ) { print ; } DESCRIPTION
This module reads a file backwards line by line. It is simple to use, memory efficient and fast. It supports both an object and a tied handle interface. It is intended for processing log and other similar text files which typically have their newest entries appended to them. By default files are assumed to be plain text and have a line ending appropriate to the OS. But you can set the input record separator string on a per file basis. OBJECT INTERFACE
These are the methods in "File::ReadBackwards"' object interface: new( $file, [$rec_sep], [$sep_is_regex] ) "new" takes as arguments a filename, an optional record separator and an optional flag that marks the record separator as a regular expression. It either returns the object on a successful open or undef upon failure. $! is set to the error code if any. readline "readline" takes no arguments and it returns the previous line in the file or undef when there are no more lines in the file. If the file is a non-seekable file (e.g. a pipe), then undef is returned. getline "getline" is an alias for the readline method. It is here for compatibility with the IO::* classes which has a getline method. eof "eof" takes no arguments and it returns true when readline() has iterated through the whole file. close "close" takes no arguments and it closes the handle tell "tell" takes no arguments and it returns the current filehandle position. This value may be used to seek() back to this position using a normal file handle. get_handle "get_handle" takes no arguments and it returns the internal Perl filehandle used by the File::ReadBackwards object. This handle may be used to read the file forward. Its seek position will be set to the position that is returned by the tell() method. Note that interleaving forward and reverse reads may produce unpredictable results. The only use supported at present is to read a file backward to a certain point, then use 'handle' to extract the handle, and read forward from that point. TIED HANDLE INTERFACE
tie( *HANDLE, 'File::ReadBackwards', $file, [$rec_sep], [$sep_is_regex] ) The TIEHANDLE, READLINE, EOF, CLOSE and TELL methods are aliased to the new, readline, eof, close and tell methods respectively so refer to them for their arguments and API. Once you have tied a handle to File::ReadBackwards the only I/O operation permissible is <> which will read the previous line. You can call eof() and close() on the tied handle as well. All other tied handle operations will generate an unknown method error. Do not seek, write or perform any other unsupported operations on the tied handle. LINE AND RECORD ENDINGS
Since this module needs to use low level I/O for efficiency, it can't portably seek and do block I/O without managing line ending conversions. This module supports the default record separators of normal line ending strings used by the OS. You can also set the separator on a per file basis. The record separator is a regular expression by default, which differs from the behavior of $/. Only if the record separator is not specified and it defaults to CR/LF (e.g, VMS, redmondware) will it will be converted to a single newline. Unix and MacOS files systems use only a single character for line endings and the lines are left unchanged. This means that for native text files, you should be able to process their lines backwards without any problems with line endings. If you specify a record separator, no conversions will be done and you will get the records as if you read them in binary mode. DESIGN
It works by reading a large(8kb) block of data from the end of the file. It then splits them on the record separator and stores a list of records in the object. Each call to readline returns the top record of the list and if the list is empty it refills it by reading the previous block from the file and splitting it. When the beginning of the file is reached and there are no more lines, undef is returned. All boundary conditions are handled correctly i.e. if there is a trailing partial line (no newline) it will be the first line returned and lines larger than the read buffer size are handled properly. NOTES
There is no support for list context in either the object or tied interfaces. If you want to slurp all of the lines into an array in backwards order (and you don't care about memory usage) just do: @back_lines = reverse <FH>. This module is only intended to read one line at a time from the end of a file to the beginning. AUTHOR
Uri Guttman, uri@stemsystems.com COPYRIGHT
Copyright (C) 2003 by Uri Guttman. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.4 2011-07-15 ReadBackwards(3pm)
All times are GMT -4. The time now is 02:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy