Thursday, September 27, 2012

Choosing random lines from a file

I have needed to do this more than once. The GNU shuf command will generate a random permutation of its input.

The following command will choose every 1/100ths of the lines from the given file. In other words if the file has 1000 lines, this command will randomly choose 10 of them.
shuf -n $(( $(wc -l $file | cut -d' ' -f1) / 100)) $file

No comments:

Post a Comment