This question already has an answer here:
这个问题已经有了答案:
- What's an easy way to read random line from a file in Unix command line? 13 answers
- 从Unix命令行中的文件读取随机行有什么简单的方法?13个答案
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