Home > HowTo's > Shuffling shell input

Shuffling shell input

April 17th, 2009 Leave a comment Go to comments

While the Fisher-Yates shuffle sounds like 1920’s dance move, it is the standard method for shuffling larger arrays of data.

The below operates like a simple Unix filter taking an array of data (separated by newlines) and returning a quickly sorted output.

# Usage: cat file | shuffle
# From: http://hashbang.net/2009/04/shuffling-shell-inputshuffling-shell-input/
# Return a shuffled version of the input stream
# Author: Matt Carter 

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];

while(<>) {
	push @input, $_;

print join("\n",@input);

For example:

ls | shuffle

Returns a randomized list of files in the current directory.

Categories: HowTo's Tags: ,
  1. July 14th, 2009 at 23:32 | #1

    had a play with this on my lenny box only to find that there is a command ‘shuf’ already installed (by default) that seems to do something similar.

    nice though.

    • July 15th, 2009 at 13:46 | #2

      Yes I discoverd Shuf too after writing this article.

      I really wish the package maintainers would add a few tags so things like this would show up in tools like ‘apropos’. ‘Shuf’ is all well and good but missing those last characters means people keep reinventing the wheel with the result above.

  1. July 7th, 2009 at 12:31 | #1