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
It is clearly biased rand (like half the time the list will start with the first line) but for some basic randomization with just bash builtins I guess it is fine? Just print each line yes/no then print the rest...
Sort:
Shuf:
Perl:
BASH:
Awk:
Here's a one-liner that doesn't rely on
shuf
orsort -R
, which I didn't have on my mac:This iterates over all the lines in
my_file
and reprints them in a randomized order.You don't need to use pipes here. Sort alone does this with the file(s) as parameters. I would just do
or if you have multiple files
You should use
shuf
command =)Or with Perl :
Just a note to OS X users who use MacPorts: the
shuf
command is part ofcoreutils
and is installed under namegshuf
.