Skip to content
shellmap

Pick a random line from a file

Select one random line from a file — useful for fortune-style picks, A/B bucketing, and test sampling.

How to pick a random line from a file in each shell

Bashunix
shuf -n 1 file.txt
Zshunix
shuf -n 1 file.txt
Fishunix
shuf -n 1 file.txt
PowerShellwindows
Get-Content file.txt | Get-Random

`Get-Random` on a string array reads ALL lines first then samples — fine for small files, OOM-risk on multi-GB. For huge files use a streaming reservoir sample (`Get-Content -ReadCount 1` + manual loop).

cmd.exewindows
powershell -NoProfile -Command "Get-Content file.txt | Get-Random"

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • macOS BSD has no `shuf`. Portable: `sort -R file.txt | head -1` (but `sort -R` groups duplicates — for unique-input files only). `gshuf -n 1 file.txt` via `brew install coreutils` is the cleanest macOS fix. Or `awk 'BEGIN{srand()} {a[NR]=$0} END{print a[int(rand()*NR)+1]}' file.txt` — works in posix awk on every Unix.
  • For a huge file where loading all lines is infeasible, use reservoir sampling: `awk 'BEGIN{srand()} {if(rand()<1/NR) line=$0} END{print line}' file.txt`. Single pass, constant memory, mathematically uniform. The classical Algorithm R from Vitter 1985 — the same trick LDB / log-processing tools use.
  • `shuf -n 1` reads the entire file before printing. For a 100 GB file that's slow + memory-heavy. Reservoir sampling (above) reads sequentially and keeps only ONE line in memory at a time — finishes in time proportional to file size, no spike. Benchmark: 10 GB file → `shuf -n 1` ~30s + 10 GB RAM; reservoir-awk ~8s + 1 line of RAM.
  • Cryptographic-quality pick: `shuf -n 1 --random-source=/dev/urandom file.txt`. pwsh: `(Get-Content file.txt)[(Get-Random -Maximum (Get-Content file.txt).Count)]` is NOT crypto (Get-Random uses non-CSPRNG seed). For crypto, generate the index via `[Security.Cryptography.RandomNumberGenerator]::GetInt32(0, $count)` (pwsh 6+) and index into the array.

Related commands

Related tasks