Sponsored Content
Full Discussion: Randomize a file
Top Forums Shell Programming and Scripting Randomize a file Post 302811063 by Chubler_XL on Thursday 23rd of May 2013 03:07:50 AM
Old 05-23-2013
Are all the records the same length? In your sample data they all appear to be 216 characters long.

If so you might be able to use this perl program, it takes 1 argument which is the name of your input file and outputs the random shuffle of the records.

Code:
#!/usr/bin/perl
my $rsz = 216;
my $fsize = -s $ARGV[0];
my $record;

my @recs = (0..($fsize/$rsz)-1);

fisher_yates_shuffle(\@recs);

open(fp, $ARGV[0]) || die;
for (my $c = 0; $c < $#recs; $c++ ) {
        seek(fp, $recs[$c]*$rsz, 0);
        read(fp, $record, $rsz);
        print $record;
}
close(fp);

sub fisher_yates_shuffle {
    my $array = shift;
    my $i;
    for ($i = @$array; --$i; ) {
        my $j = int rand ($i+1);
        next if $i == $j;
        @$array[$i,$j] = @$array[$j,$i];
    }
}


Last edited by Chubler_XL; 05-23-2013 at 06:38 AM..
 

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Randomize letters

Hi, Is there a tool somewhat parallel to rev, but which randomizes instead of reverses? I've tried rl, but I can only get it to randomize words. I was hoping for something like this echo "hello" | ran leolh less simpler solutions are also welcome. Sorry if the question is... (21 Replies)
Discussion started by: jeppe83
21 Replies

2. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

3. Shell Programming and Scripting

Randomize a matrix

--please have a look at my third post in this thread! there I explained it more clearly-- Hey guys. I posted a complex problem few days back. No reply! :| Here is simplified question: I have a matrix with 0/1: * col1 col2 col3 row1 1 0 1 row2 0 0 ... (5 Replies)
Discussion started by: @man
5 Replies

4. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

5. Shell Programming and Scripting

Randomize columns in CSV file

Hi there, friends! Writing exams again! This time my wish would be to randomize certain columns in a csv file. Given a file containing records consisting of 3 columns tab-separated: A B C A B C A B C I would love to get the columns of each record in random order...separated by a tab as... (12 Replies)
Discussion started by: eldeingles
12 Replies

6. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies
DBASE_ADD_RECORD(3)							 1						       DBASE_ADD_RECORD(3)

dbase_add_record - Adds a record to a database

SYNOPSIS
bool dbase_add_record (int $dbase_identifier, array $record) DESCRIPTION
Adds the given data to the database. PARAMETERS
o $dbase_identifier - The database link identifier, returned by dbase_open(3) or dbase_create(3). o $record - An indexed array of data. The number of items must be equal to the number of fields in the database, otherwise dbase_add_record(3) will fail. Note If you're using dbase_get_record(3) return value for this parameter, remember to reset the key named deleted. RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 Inserting a record in a dBase database <?php // open in read-write mode $db = dbase_open('/tmp/test.dbf', 2); if ($db) { dbase_add_record($db, array( date('Ymd'), 'Maxim Topolov', '23', 'max@example.com', 'T')); dbase_close($db); } ?> SEE ALSO
dbase_delete_record(3), dbase_replace_record(3). PHP Documentation Group DBASE_ADD_RECORD(3)
All times are GMT -4. The time now is 11:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy