grep — Pipelines
grep + xargs
grep + xargs — act on matched files
grep -rlP ':toc:' --include='*.adoc' docs/ | xargs -I{} sed -i '/:toc:/d' {}
# Find and remove :toc: from all files (STD-004 remediation)
Find and replace across files
grep -rl 'old_api' src/ | xargs sed -i 's/old_api/new_api/g'
grep + xargs — act on matched files with confirmation
grep -rlP 'hardcoded-ip' docs/ | xargs -p -I{} sed -i 's/10.50.1.50/{ise-primary-ip}/g' {}
grep + awk
grep + awk — extract and rank failed SSH login IPs
grep -P 'Failed password' /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -rn | head
grep + awk — find files with >5 cross-references
grep -rcP --include='*.adoc' 'xref:' docs/modules/ROOT/pages/ | awk -F: '$2 > 5'
find + grep
find + grep — search with filesystem predicates
find docs/ -name '*.adoc' -mtime -7 -exec grep -lP 'TODO' {} +
# Recently modified files with TODOs
grep + process substitution — diff two searches
diff <(grep -rlP 'include::partial\$' docs/modules/ROOT/pages/) \
<(grep -rlP 'include::example\$' docs/modules/ROOT/pages/)
# Pages that use partials vs examples
Conditional Scripting
Quiet test in conditionals
grep -qP 'PermitRootLogin\s+no' /etc/ssh/sshd_config && echo "secure" || echo "FIX THIS"
Guard for idempotent operations
grep -qF '10.50.1.91 mail-01' /etc/hosts || echo '10.50.1.91 mail-01.inside.domusdigitalis.dev mail-01' | sudo tee -a /etc/hosts
Process matching files in a loop
grep -rlP --include='*.adoc' '^\|.*`[^`]*(?<!\\)\|[^`]*`' docs/ | while read -r f; do
echo "=== Unescaped pipes in: $f ==="
grep -nP '^\|.*`[^`]*(?<!\\)\|[^`]*`' "$f"
done
Exit code in pipelines — grep returns 0 (found), 1 (not found), 2 (error)
if grep -qP 'ERROR|FATAL' /var/log/app.log; then
systemctl restart app
fi
Runbook Navigation
Find all phases
grep -n '^=== Phase' runbook.adoc
Find phase with 30 lines of context
grep -n -A30 '^=== Phase 3:' runbook.adoc
List phase titles only
grep -oE '^=== Phase [0-9]+:.*' runbook.adoc
Count phases
grep -c '^=== Phase' runbook.adoc
Show all section headers (document outline)
grep -nE '^={2,4} ' runbook.adoc
Find pre/post validation sections
grep -n -A10 'Pre-Validation' runbook.adoc
grep -n -A10 'Post-Validation' runbook.adoc