在bash [duplicate]中从文件中选择随机行

时间:2022-07-22 21:32:50

This question already has an answer here:

这个问题已经有了答案:

in bash script I want to pick out N random lines from input file and output to another file. how can this be done?

在bash脚本中,我想从输入文件和输出到另一个文件中选择N个随机行。怎么做呢?

2 个解决方案

#1


347  

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

使用shuf和-n选项,如下所示,得到N个随机行:

shuf -n N input > output

#2


136  

Sort the file randomly and pick first 100 lines:

随机对文件进行排序,选择前100行:

$ sort -R input | head -n 100 >output

#1


347  

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

使用shuf和-n选项,如下所示,得到N个随机行:

shuf -n N input > output

#2


136  

Sort the file randomly and pick first 100 lines:

随机对文件进行排序,选择前100行:

$ sort -R input | head -n 100 >output