Select random lines from a file

user121196 picture user121196 · Feb 12, 2012 · Viewed 162.5k times · Source

In a Bash script, I want to pick out N random lines from input file and output to another file.

How can this be done?

Answer

dogbane picture dogbane · Feb 25, 2013

Use shuf with the -n option as shown below, to get N random lines:

shuf -n N input > output