Line Selection
Print first 10 lines — head emulation
awk 'NR<=10' /etc/passwd
Print last 10 lines — tail emulation via circular buffer
awk '{a[NR%10]=$0} END{for(i=NR+1;i<=NR+10;i++) print a[i%10]}' /etc/passwd
Print lines 20 through 30 — sed-like range
awk 'NR>=20 && NR<=30' /etc/passwd
Print every other line (odd lines)
cat <<'EOF' > /tmp/demo.txt
10.50.1.1 pfsense firewall
10.50.1.20 ise-01 pan
10.50.1.50 ad-dc dc
10.50.1.90 bind dns
10.50.1.60 vault secrets
10.50.1.70 vyos router
EOF
awk 'NR%2==1' /tmp/demo.txt
Print lines longer than 80 characters
ps aux | awk 'length($0)>80'
Print lines with more than 5 fields
ps aux | awk 'NF>5'
Print non-empty lines (skip blanks)
cat <<'EOF' > /tmp/demo.txt
10.50.1.1 pfsense
10.50.1.20 ise-01
10.50.1.50 ad-dc
EOF
awk 'NF' /tmp/demo.txt
Skip comments and blank lines
cat <<'EOF' > /tmp/demo.conf
# Upstream DNS servers
nameserver 10.50.1.90
# Fallback
nameserver 1.1.1.1
# Local domain
search inside.domusdigitalis.dev
EOF
awk '/^#/{next} NF{print}' /tmp/demo.conf
Sample every 100th line from a large file
journalctl --no-pager -q | awk 'NR%100==0'
Print line count per file when processing multiple files
echo -e "alpha\nbravo\ncharlie" > /tmp/demo1.txt
echo -e "delta\necho" > /tmp/demo2.txt
echo -e "foxtrot\ngolf\nhotel\nindia" > /tmp/demo3.txt
awk 'FNR==1 && NR>1{print prev, count} {prev=FILENAME; count=FNR} END{print prev, count}' /tmp/demo1.txt /tmp/demo2.txt /tmp/demo3.txt