April 18, 2013

HOWTO: Get a random sampling of lines from a text file in bash

Let's say you needed to get 150 lines at random from a text file and you were running Linux.

The solution involves a command I wasn't aware of until today...shuf.

The command:

shuf file-to-sample.txt | head -n 150

shuf scrambles all the lines in the file; head pulls the set number of lines for you.

No comments: