#!/usr/bin/env zsh
# ── zgrep.zsh : grep INSIDE compressed files without unpacking them ──────────
# Rotated logs (.gz), archived captures, *.xz/*.bz2/*.zst — zgrep decompresses
# on the fly and greps the stream. Same flags as grep. The whole z-family:
# zgrep/zegrep/zfgrep · zcat/zless/zmore · bzgrep xzgrep zstdgrep (per fmt)
#
# zgrepi <t> f.gz case-insensitive search in a .gz
# zg <t> *.gz short alias for zgrep
# zgrep-cheat this card
# Modern shortcut: `rg -z PATTERN` searches gz/bz2/xz/zst/lz4 RECURSIVELY —
# often what you actually want across a directory of archives.
if command -v zgrep >/dev/null 2>&1; then
alias zg='zgrep'
alias zgrepi='zgrep -i'
alias zgrepn='zgrep -n'
fi
zgrepcheat() {
print -P ""
print -P "%F{#9ccfd8}▰▰▶ ZGREP CHEAT SHEET%f %F{#908caa}search inside compressed files%f"
print -P "%F{#908caa}──────────────────────────────────────────────────────────────%f"
print -P "%F{#f6c177} idea%f zgrep = %F{#e0def4}zcat file | grep%f, but handles many files & keeps names"
print -P "%F{#f6c177} everyday%f"
print -P " %F{#e0def4}zgrep -i error /var/log/*.gz%f scan all gzip'd logs, case-insens."
print -P " %F{#e0def4}zgrep pat access.log*.gz%f across rotated logs at once"
print -P " %F{#e0def4}zgrep -c pat file.gz%f count matches %F{#e0def4}-n%f line numbers"
print -P " %F{#e0def4}zgrep -l pat *.gz%f which archives contain it"
print -P "%F{#f6c177} flags%f same as grep — %F{#e0def4}-i -n -c -l -v -E -o -A/-B/-C%f all pass through"
print -P "%F{#f6c177} pick the tool for the format%f"
print -P " %F{#e0def4}zgrep%f .gz (gzip) %F{#e0def4}bzgrep%f .bz2"
print -P " %F{#e0def4}xzgrep%f .xz / .lzma %F{#e0def4}zstdgrep%f .zst"
print -P "%F{#f6c177} just view / dump%f"
print -P " %F{#e0def4}zcat f.gz%f dump %F{#e0def4}zless f.gz%f page %F{#e0def4}zmore f.gz%f"
print -P "%F{#f6c177} the modern one-liner (recursive, all formats)%f"
print -P " %F{#e0def4}rg -z pattern ./logs%f rg reads gz/bz2/xz/zst/lz4 while recursing"
print -P "%F{#908caa}──────────────────────────────────────────────────────────────%f"
print -P "%F{#908caa} zgrep = one/many named archives · %f%F{#e0def4}rg -z%f%F{#908caa} = sweep a whole tree of them%f"
}
alias zgrep-cheat='zgrepcheat'