Shell Output Filtering for Token Efficiency

lesson tools active tools/shell-output-filtering.md View on GitHub

Shell Output Filtering for Token Efficiency

Rule

Always filter shell output with grep/head/tail when expecting large results (>1000 lines).

Context

When running shell commands that produce extensive output like logs, file listings, or data dumps.

Detection

Observable signals that filtering is needed:

Pattern

Filter output before it enters context:

# Wrong: Dump entire log (could be 10k+ lines)
cat large-log.log

# Correct: Filter to relevant sections
grep "ERROR" large-log.log | tail -50

# Wrong: List all files
ls -la /var/log/

# Correct: Filter by pattern or limit count
ls -la /var/log/*.error | head -20

# Wrong: Full JSON dump
cat huge-config.json

# Correct: Extract specific fields
cat huge-config.json | jq '.relevant.section'

Outcome

Following this pattern results in:

Examples:

Related

Match Keywords

filter output with grep head tail command dumped 10k+ tokens large output 1000+ lines token efficiency shell