Sponsored Content
Top Forums Shell Programming and Scripting randomly shuffle two text files the same way Post 302551840 by Corona688 on Wednesday 31st of August 2011 08:21:44 PM
Old 08-31-2011
Combine them into one line of two things with paste, shuffle them with shuf, separate them out into two files again. I've done it with awk but you could do it many ways.
Code:
$ paste -d : file1 file2 | shuf
five:5
two:2
one:1
four:4
three:3
$ paste -d ':' file1 file2 | shuf | awk -v FS=":" '{ print $1 > "out1" ; print $2 > "out2" }'

Your output files shouldn't be the same as your input files. If you need it to be, just rename the new files overtop of the old.

I didn't know about the shuf command. Thanks.
This User Gave Thanks to Corona688 For This Post:
 

8 More Discussions You Might Find Interesting

1. AIX

Randomly appearing control characters in text files

Hi, From some time, we have noticed that our ascii files have started corrupting due to the presence of some random control characters (^@, ^M, ^H, ^D). The characters appear randomly on any file after the process that creates the file finishes. If we rerun the process, the files re creates... (0 Replies)
Discussion started by: aakashahuja
0 Replies

2. UNIX for Dummies Questions & Answers

randomly renaming files

I have a directory of files that look like filename 001.ext, filename 002.ext, etc. I'd like to rename the files with unique random numbered names, so that the original filenames are stripped and the files are given a new, random number name. I'm not super new to UNIX, but I don't often use it for... (2 Replies)
Discussion started by: platz
2 Replies

3. Shell Programming and Scripting

shuffle pack of words in line

hello i just seeking for a simple way to make a shuffle by block of words in a line. no matter shell (sh/bash) or perl should be like this: the message (which is line of some file) splits to packs (packs are random 5-10 words in each) then making a new line inserting those packs in a random... (9 Replies)
Discussion started by: tip78
9 Replies

4. UNIX for Dummies Questions & Answers

How to randomly select lines from a text file

I have a text file with 1000 lines, I want to randomly select 200 lines from it and print them as output. How do I go about doing that? Thanks! (7 Replies)
Discussion started by: evelibertine
7 Replies

5. Shell Programming and Scripting

Randomly selecting sequences and generating specific output files

I have two files containing hundreds of different sequences with the same Identifiers (ID-001, ID-002, etc.,), something like this: Infile1: ID-001 ATGGGAGCGGGGGCGTCTGCCTTGAGGGGAGAGAAGCTAGATACA ID-002 ATGGGAGCGGGGGCGTCTGTTTTGAGGGGAGAGAAGCTAGATACA ID-003... (18 Replies)
Discussion started by: Xterra
18 Replies

6. Shell Programming and Scripting

How to shuffle odd and even columns?

Is there any way to place each even column name infront of its odd column using awk or others? input Ab name MGH26 B04 MGH26 B05 output name_Ab B04_MGH26 B05_MGH26 (4 Replies)
Discussion started by: quincyjones
4 Replies

7. Shell Programming and Scripting

Randomized shuffle words on each line

Hi Folks, I have a text file with a thousand lines consisting of words or a group of words separated by commas. I would like to randomize / shuffle the words on each line. Eg; file.txt Linux,Open,Free,Awesome,Best Things in Life,The Greatest Laptop,PC,Tablet,Home Computers,Digital... (2 Replies)
Discussion started by: martinsmith
2 Replies

8. UNIX for Beginners Questions & Answers

Logic shuffle

Hi, Is there any way I can shuffle the numbers randomly. I have been trying to google and I found lots of 'generator' but is it possible to find the background logic to create randomness? Thanks, (3 Replies)
Discussion started by: Indra2011
3 Replies
Data::Entropy::Algorithms(3pm)				User Contributed Perl Documentation			    Data::Entropy::Algorithms(3pm)

NAME
Data::Entropy::Algorithms - basic entropy-using algorithms SYNOPSIS
use Data::Entropy::Algorithms qw(rand_bits rand_int rand_prob); $str = rand_bits(17); $i = rand_int(12345); $i = rand_int(Math::BigInt->new("1000000000000")); $j = rand_prob(1, 2, 3); $j = rand_prob([ 1, 2, 3 ]); use Data::Entropy::Algorithms qw(rand_fix rand rand_flt); $x = rand_fix(48); $x = rand(7); $x = rand_flt(0.0, 7.0); use Data::Entropy::Algorithms qw(pick pick_r choose choose_r shuffle shuffle_r); $item = pick($item0, $item1, $item2); $item = pick_r(@items); @chosen = choose(3, $item0, $item1, $item2, $item3, $item4); $chosen = choose_r(3, @items); @shuffled = shuffle($item0, $item1, $item2, $item3, $item4); $shuffled = shuffle_r(@items); DESCRIPTION
This module contains a collection of fundamental algorithms that use entropy. They all use the entropy source mechanism described in Data::Entropy. FUNCTIONS
All of these functions use entropy. The entropy source is not an explicit input in any case. All functions use the current entropy source maintained by the "Data::Entropy" module. To select an entropy source use the "with_entropy_source" function in that module, or alternatively do nothing to use the default source. Fundamental entropy extraction rand_bits(NBITS) Returns NBITS bits of entropy, as a string of octets. If NBITS is not a multiple of eight then the last octet in the string has its most significant bits set to zero. rand_int(LIMIT) LIMIT must be a positive integer. Returns a uniformly-distributed random integer in the range [0, LIMIT). LIMIT may be either a native integer, a "Math::BigInt" object, or an integer-valued "Math::BigRat" object; the returned number is of the same type. rand_prob(PROB ...) rand_prob(PROBS) Returns a random integer selected with non-uniform probability. The relative probabilities are supplied as a list of non-negative integers (multiple PROB arguments) or a reference to an array of integers (the PROBS argument). The relative probabilities may be native integers, "Math::BigInt" objects, or integer-valued "Math::BigRat" objects; they must all be of the same type. At least one probability value must be positive. The first relative probability value (the first PROB or the first element of PROBS) is the relative probability of returning 0. The absolute probability of returning 0 is this value divided by the total of all the relative probability values. Similarly the second value controls the probability of returning 1, and so on. Numbers rand_fix(NBITS) Returns a uniformly-distributed random NBITS-bit fixed-point fraction in the range [0, 1). That is, the result is a randomly-chosen multiple of 2^-NBITS, the multiplier being a random integer in the range [0, 2^NBITS). The value is returned in the form of a native floating point number, so NBITS can be at most one greater than the number of bits of significand in the floating point format. With NBITS = 48 the range of output values is the same as that of the Unix "drand48" function. rand([LIMIT]) Generates a random fixed-point fraction by "rand_fix" and then multiplies it by LIMIT, returning the result. LIMIT defaults to 1, and if it is 0 then that is also treated as 1. The length of the fixed-point fraction is 48 bits, unless that can't be represented in the native floating point type, in which case the longest possible fraction will be generated instead. This is a drop-in replacement for "CORE::rand": it produces exactly the same range of output values, but using the current entropy source instead of a sucky PRNG with linear relationships between successive outputs. ("CORE::rand" does the type of calculation described, but using the PRNG "drand48" to generate the fixed-point fraction.) The details of behaviour may change in the future if the behaviour of "CORE::rand" changes, to maintain the match. Where the source of a module can't be readily modified, it can be made to use this "rand" by an incantation such as *Foreign::Module::rand = &Data::Entropy::Algorithms::rand; This must be done before the module is loaded, most likely in a "BEGIN" block. It is also possible to override "CORE::rand" for all modules, by performing this similarly early: *CORE::GLOBAL::rand = &Data::Entropy::Algorithms::rand; This function should not be used in any new code, because the kind of output supplied by "rand" is hardly ever the right thing to use. The "int(rand($n))" idiom to generate a random integer has non-uniform probabilities of generating each possible value, except when $n is a power of two. For floating point numbers, "rand" can't generate most representable numbers in its output range, and the output is biased towards zero. In new code use "rand_int" to generate integers and "rand_flt" to generate floating point numbers. rand_flt(MIN, MAX) Selects a uniformly-distributed real number (with infinite precision) in the range [MIN, MAX] and then rounds this number to the nearest representable floating point value, which it returns. (Actually it is only as if the function worked this way: in fact it never generates the number with infinite precision. It selects between the representable floating point values with the probabilities implied by this process.) This can return absolutely any floating point value in the range [MIN, MAX]; both MIN and MAX themselves are possible return values. All bits of the floating point type are filled randomly, so the range of values that can be returned depends on the details of the floating point format. (See Data::Float for low-level floating point utilities.) The function "die"s if MIN and MAX are not both finite. If MIN is greater than MAX then their roles are swapped: the order of the limit parameters actually doesn't matter. If the limits are identical then that value is always returned. As a special case, if the limits are positive zero and negative zero then a zero will be returned with a randomly-chosen sign. Combinatorics pick(ITEM ...) Randomly selects and returns one of the ITEMs. Each ITEM has equal probability of being selected. pick_r(ITEMS) ITEMS must be a reference to an array. Randomly selects and returns one of the elements of the array. Each element has equal probability of being selected. This is the same operation as that performed by "pick", but using references to avoid expensive copying of arrays. choose(NCHOOSE, ITEM ...) Randomly selects NCHOOSE of the ITEMs. Each ITEM has equal probability of being selected. The chosen items are returned in a list in the same order in which they appeared in the argument list. choose_r(NCHOOSE, ITEMS) ITEMS must be a reference to an array. Randomly selects NCHOOSE of the elements in the array. Each element has equal probability of being selected. Returns a reference to an array containing the chosen items in the same order in which they appeared in the input array. This is the same operation as that performed by "choose", but using references to avoid expensive copying of arrays. shuffle(ITEM ...) Reorders the ITEMs randomly, and returns them in a list in random order. Each possible order has equal probability. shuffle_r(ITEMS) ITEMS must be a reference to an array. Reorders the elements of the array randomly. Each possible order has equal probability. Returns a reference to an array containing the elements in random order. This is the same operation as that performed by "shuffle", but using references to avoid expensive copying of arrays. SEE ALSO
Data::Entropy, Data::Entropy::Source AUTHOR
Andrew Main (Zefram) <zefram@fysh.org> COPYRIGHT
Copyright (C) 2006, 2007, 2009, 2011 Andrew Main (Zefram) <zefram@fysh.org> LICENSE
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.3 2011-05-09 Data::Entropy::Algorithms(3pm)
All times are GMT -4. The time now is 03:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy