Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly shuffling lines in Linux / Bash

Tags:

linux

bash

shell

sh

I have some files in linux. For example 2 and i need shuffling the files in one file.

For example

$cat file1 line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 

and

$cat file2 linea one linea two linea three linea four linea five linea six linea seven linea eight 

And later that i shuffling the two files i can obtain something like:

linea eight line 4 linea five line 1 linea three line 8 linea seven line 5 linea two linea one line 2 linea four line 7 linea six line 1 line 6 
like image 276
Code Geas Coder Avatar asked Jul 10 '13 19:07

Code Geas Coder


People also ask

How do I randomize a file in Linux?

Assemble the Command. The sort command in the middle sorts the prepared line by the first field, which contains the random numbers we prepended. Let's use the powerful awk command to prepend a random number on each line: $ awk 'BEGIN{srand()}{print rand(), $0}' input.

What is Shuf command?

The shuf command in Linux writes a random permutation of the input lines to standard output. It pseudo randomizes an input in the same way as the cards are shuffled.

What is $s in bash?

From man bash : -s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.


2 Answers

You should use shuf command =)

cat file1 file2 | shuf 

Or with Perl :

cat file1 file2 | perl -MList::Util=shuffle -wne 'print shuffle <>;' 
like image 158
Gilles Quenot Avatar answered Oct 21 '22 06:10

Gilles Quenot


Sort: (similar lines will be put together)

cat file1 file2 | sort -R 

Shuf:

cat file1 file2 | shuf 

Perl:

cat file1 file2 | perl -MList::Util=shuffle -e 'print shuffle<STDIN>' 

BASH:

cat file1 file2 | while IFS= read -r line do     printf "%06d %s\n" $RANDOM "$line" done | sort -n | cut -c8- 

Awk:

cat file1 file2 | awk 'BEGIN{srand()}{printf "%06d %s\n", rand()*1000000, $0;}' | sort -n | cut -c8- 
like image 41
jm666 Avatar answered Oct 21 '22 05:10

jm666