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