Wi-Fizzle.com - Putting the fizzle in Wi-Fi since 2005 .. (yes, this was a poor choice for a domain name)

<div class="news_item">#270<div class="news_title">One-liners for randomizing the order of lines in a file

Posted by dandriff on Tuesday June 24, 2008@10:16AM

One-liners for randomizing the order of lines in a file:

Using only the Bash command line:
<pre>cat dead.letter | while read -r line; do echo "$RANDOM $line"; done | sed 's/^/0000/' | sed 's/^0*\([0-9]\{5\}[ ].*$\)/\1/' | sort | sed -r 's/^[0-9]+ //'</pre>

Using Perl:
<pre>perl -e '$filename="dead.letter";use List::Util "shuffle";open FILE,"<$filename" or die("unable to open $filename for reading\n");print shuffle(<FILE>);'</pre>

Even shorter with Perl:
<pre>cat dead.letter | perl -e '@a=<>;while(@a){print splice(@a,rand(@a),1)}'</pre>