randomly shuffle two text files the same way


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting randomly shuffle two text files the same way
# 1  
Old 08-31-2011
randomly shuffle two text files the same way

What I have are two text files that I need to shuffle randomly, but I need the two files to be randomly shuffled the same way. I have heard of shuf but I do not know how to use it for two files. Maybe there is also an easy/simple awk command I do not know about that could handle this problem.

for example:
before shuffling
Code:
file 1:
one
two
three
four
five



file 2:
1
2
3
4
5

and after shuffling

Code:
file 1:
five
one
four
three
two


file 2:
5
1
4
3
2

As you can see the lines still match up across the two files (ofcourse this is just an example, I need the output to be random).

Last edited by adrunknarwhal; 08-31-2011 at 09:11 PM.. Reason: forgot to add something
# 2  
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:
# 3  
Old 08-31-2011
Thank you for the help. I was looking at the paste command while I was waiting for a response. I was close to figuring it out but I was having trouble separating the lines after I shuffled them. Thanks again.
# 4  
Old 08-31-2011
Another idea if your shuf supports --random-source

Code:
$ dd if=/dev/random of=myrand count=1024
 
$ shuf --random-source=myrand file1
one
three
five
four
two

$ shuf --random-source=myrand file2
1
3
5
4
2

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

8. 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
Login or Register to Ask a Question