shuf

テキスト処理

書き方

shuf [file]

行を無作為な順で出します。GNU coreutils なので macOS には無く、代わりの sort -R は同じ行をまとめてしまいます。

よく使うオプション

オプション意味
-nPrint only N lines
-eTreat the arguments themselves as the lines
-iDraw from a number range, as in -i 1-100
-rAllow the same line to come out more than once
-oWrite to a file instead of standard output
--random-sourceUse a fixed source so the shuffle can be repeated

shuf -n 1 names.txt

Picks one line at random.

shuf -i 1-49 -n 6

Six different numbers between 1 and 49.

sort -R names.txt

The 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