Wednesday, March 28, 2012

Scrambling/Unsorting a file in Linux

Hi guys,

No doubt, linux terminal is a important tool for programmers. Its simply beautiful. We always sometimes need to sort a file (linux sort is here) and sometimes we need to find the number of lines in a file whereby we uses the wc command. However, today my task was a bit different.

We had a a file containg a list of numbers and we had to scramble the file random such that all the lines gets to new location with the file randomly. To do this, there is two commads

1. unsort
2. shuf

I don't know if the unsort commad is install on your pc. If not then simply do and "apt-get intall unsort" or "yum install sort".

1. unsort
To be frank, I'm not a great fan of the commands of unsort even if i used shuf command only few times. i may be wrong, but what I notices with unsort is that it breaks the list into portions and then try to perforn a shuffle within that portion. Even though, to unsort a file, simply

unsort <inputfile>



2. shuf

if you want to fully understand how to use shuf, do a "man shuf" to see the manual for shuf. I'll simply demonstrate a simply example

shuf <inputfile> -o <outputfile>, here shuf will take the input file, shuffle it using the default random and output the file
 
 shuf  --random-source=/dev/urandom  <inputfile> -o <outputfile>, here the same operation will be performed except that the random numbers are from /dev/urandom from linux

No comments:

Post a Comment