Range Patterns
Print between two patterns (inclusive)
cat <<'EOF' > /tmp/deploy-log.txt
pre-flight: checking dependencies
BEGIN deployment vault-02
step 1: stop service
step 2: backup data
step 3: upgrade binary
step 4: start service
END deployment vault-02
post-deploy: smoke tests passed
EOF
awk '/BEGIN/,/END/' /tmp/deploy-log.txt
Extract YAML frontmatter between --- markers
cat <<'EOF' > /tmp/page.md
---
title: Vault PKI Setup
date: 2026-04-11
tags: [vault, pki, certificates]
---
# Vault PKI Setup
This document covers intermediate CA enrollment.
EOF
awk '/^---$/,/^---$/' /tmp/page.md
State machine — print section, skip the markers themselves
cat <<'EOF' > /tmp/mixed.conf
# Global settings
log_level = info
max_connections = 500
BEGIN_CONFIG
listen_address = 10.50.1.30
port = 8200
tls_cert = /etc/ssl/certs/vault.pem
tls_key = /etc/ssl/private/vault.key
END_CONFIG
# Post-config hooks
notify_admin = true
EOF
awk '/^BEGIN_CONFIG/{p=1; next} /^END_CONFIG/{p=0} p' /tmp/mixed.conf
State machine — inclusive of start marker
cat <<'EOF' > /tmp/runbook.txt
prerequisites completed
START provisioning ise-01
configure radius
enroll certificate
join AD domain
enable 802.1X
END provisioning ise-01
verification pending
EOF
awk '/START/{p=1} p; /END/{p=0}' /tmp/runbook.txt
Extract man page section using state machine (col -b strips formatting)
man awk | col -b | awk '/^EXAMPLES/{p=1; next} /^[A-Z]/ && p{exit} p'
Accumulate lines between markers into single record
cat <<'EOF' > /tmp/records.txt
---
hostname: vault-01
ip: 10.50.1.30
role: secrets
---
hostname: ise-01
ip: 10.50.1.20
role: radius
---
hostname: bind-01
ip: 10.50.1.40
role: dns
EOF
awk '/^---$/{if(buf) print buf; buf=""; next} {buf = buf ? buf " | " $0 : $0} END{if(buf) print buf}' /tmp/records.txt
Track nesting depth — count brace pairs
cat <<'EOF' > /tmp/source.c
int main() {
if (connected) {
while (running) {
process_request();
}
}
return 0;
}
EOF
awk '/{/{depth++} depth>0{print depth, $0} /}/{depth--}' /tmp/source.c
Print everything EXCEPT between markers (negated range)
cat <<'EOF' > /tmp/config-full.txt
# Network settings
listen_address = 0.0.0.0
port = 443
BEGIN deprecated_section
old_cipher = DES
legacy_mode = true
END deprecated_section
# TLS settings
tls_min_version = 1.2
cipher_suite = ECDHE-RSA-AES256-GCM-SHA384
EOF
awk '/BEGIN/,/END/{next} {print}' /tmp/config-full.txt
Extract SSH config blocks for a specific host
awk '/^Host vault-01/{p=1; print; next} /^Host /{p=0} p' ~/.ssh/config