Is there any easy command or application that will randomise lines in a text file?
November 12, 2004 6:55 AM
Subscribe
Is there any easy command or application that will randomise lines in a text file?
Answer: yes, and as usual it turns up immediately after asking the question. For those interested:
#!/usr/bin/perl
# Program to output lines of file in random order
# Read File
while (<>) {
push(foo, $_);
}
# Shuffle lines
srand;
for (0..$#foo) {
$r = rand($#foo+1);
($foo[$r], $foo[$_]) = ($foo[$_], $foo[$r]);
}
# Print it out
for (0..$#foo) {
print $foo[$_];
}
perl [inputfile] > [outputfile]
My thanks to
Steve who answered me seven and a half years ago.>
posted by humuhumu to technology (11 comments total)
perl -ne '$l = $_ if rand() < 1/$.; END { print $l }' your_file
posted by nicwolff at 7:19 AM on November 12, 2004