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