Select random lines from a file [duplicate]

随声附和 提交于 2019-11-26 10:07:53

问题


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

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?


回答1:


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

shuf -n N input > output



回答2:


Sort the file randomly and pick first 100 lines:

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


来源:https://stackoverflow.com/questions/9245638/select-random-lines-from-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!