$ sample 10 somelongfile.txt ## to get 10 random lines $ some long command | sample 50 > mysample.txt #### my $wanted = shift || 10; my @got; die "Invalid number of lines!\n" if $wanted < 1; while (<>) { if (@got < $wanted) { push @got, $_; } elsif (rand($.) < $wanted) { splice @got, rand(@got), 1; push @got, $_; } } die "Not enough lines!\n" if @got < $wanted; print @got;