从文件中读取随机行的简单方法是什么? [英] What's an easy way to read random line from a file?

查看:25
本文介绍了从文件中读取随机行的简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 shell 脚本中从文件中读取随机行的简单方法是什么?

What's an easy way to read random line from a file in a shell script?

推荐答案

可以使用shuf:

shuf -n 1 $FILE

还有一个名为 rl 的实用程序.在 Debian 中,它位于 randomize-lines 包中,可以完全满足您的需求,尽管并非在所有发行版中都可用.在它的主页上,它实际上建议使用 shuf 代替(我相信它在创建时并不存在).shuf 是 GNU coreutils 的一部分,rl 不是.

There is also a utility called rl. In Debian it's in the randomize-lines package that does exactly what you want, though not available in all distros. On its home page it actually recommends the use of shuf instead (which didn't exist when it was created, I believe). shuf is part of the GNU coreutils, rl is not.

rl -c 1 $FILE

这篇关于从文件中读取随机行的简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆