shuf
文本处理
用法
shuf [file]把输入的行按随机顺序输出;它属于 GNU coreutils,macOS 上没有,而常用替代 sort -R 会把相同的行聚在一起。
常用选项
| 选项 | 含义 |
|---|---|
| -n | Print only N lines |
| -e | Treat the arguments themselves as the lines |
| -i | Draw from a number range, as in -i 1-100 |
| -r | Allow the same line to come out more than once |
| -o | Write to a file instead of standard output |
| --random-source | Use a fixed source so the shuffle can be repeated |
示例
shuf -n 1 names.txtPicks one line at random.
shuf -i 1-49 -n 6Six different numbers between 1 and 49.
sort -R names.txtThe nearest thing on a machine without shuf.
它们本来就是用管道串起来用的。串三条往往比让一条命令做完所有事更短。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. shuf 是做什么的?
把输入的行按随机顺序输出;它属于 GNU coreutils,macOS 上没有,而常用替代 sort -R 会把相同的行聚在一起。
Q. 怎么写?
shuf [file] —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 6 个,完整列表在 man shuf。这条命令属于文本处理。
相关命令
man 手册: man shuf