//View Tip #557
Similar Tips
» List non-system users
» Top ten running processes
» Find and replace with backup
» Sort a file by line length
» Bash function to decompress archives

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
The bash shell has a variable called $RANDOM, which outputs a pseudo-random number every time you call it. This allows you to randomize the lines in a file for example:

    for i in `cat unusual.txt`; do echo "$RANDOM $i"; done | sort | sed -r 's/^[0-9]+ //' > randorder.txt

In other words, put a random number on every line, sort the file, then take off the random numbers.


View Comments »




Comments 

Add your comment

Comments are currently disabled
Jonas
There is a error in your coded. Replace the single quote with double so it reads "$RANDOM $i"
Posted 2009-03-10 11:19:53
Thanks Jonas for pointing that out, must have missed that one in moderation! The command is corrected now.
Posted 2009-03-10 11:25:07
John
$RANDOM is undoubtedly useful, but a much quicker way to accomplish what this script is doing would be:

sort -R unusual.txt > randorder.txt
Posted 2009-03-10 13:03:53
for i in $(cat unusual.txt); ...

Backticks are the enemy.
Posted 2009-03-11 20:17:38

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit