Built-in Variables

Print line numbers with content (NR)
cat <<'EOF' > /tmp/demo.txt
10.50.1.20  ise-01   active
10.50.1.50  ad-dc    active
10.50.1.1   pfsense  standby
10.50.1.90  bind     active
EOF
awk '{print NR, $0}' /tmp/demo.txt
Show field count per line (NF)
cat <<'EOF' > /tmp/demo.txt
10.50.1.20  ise-01   active
10.50.1.50  ad-dc
10.50.1.1   pfsense  standby  primary
EOF
awk '{print NR": "NF" fields"}' /tmp/demo.txt
Print only line 5 (NR selection)
awk 'NR==5' /etc/passwd
Print lines 5 through 10
awk 'NR>=5 && NR<=10' /etc/passwd
Skip header line
cat <<'EOF' > /tmp/demo.txt
hostname,ip,status,role
ise-01,10.50.1.20,active,pan
ad-dc,10.50.1.50,active,dc
pfsense,10.50.1.1,active,firewall
EOF
awk 'NR>1' /tmp/demo.txt
Count total lines — equivalent to wc -l
awk 'END{print NR}' /etc/passwd
Last field of each line ($NF)
ss -tlnp 2>/dev/null | awk '{print $NF}'
Set input field separator in BEGIN block
awk 'BEGIN{FS=":"} {print $1, $3}' /etc/passwd
Change output separator — semicolons between records
cat <<'EOF' > /tmp/demo.txt
ise-01
ad-dc
pfsense
bind
vyos
EOF
awk 'BEGIN{ORS="; "} {print $1}' /tmp/demo.txt
Multi-character field separator
cat <<'EOF' > /tmp/demo.txt
ise-01::10.50.1.20::active
ad-dc::10.50.1.50::active
pfsense::10.50.1.1::standby
EOF
awk 'BEGIN{FS="::"} {print $1, $2}' /tmp/demo.txt
Track filename when processing multiple files
echo -e "alpha\nbravo" > /tmp/demo1.txt
echo -e "charlie\ndelta" > /tmp/demo2.txt
awk 'FNR==1 {print "=== " FILENAME " ==="} {print FNR, $0}' /tmp/demo1.txt /tmp/demo2.txt
Format /etc/passwd as CSV using BEGIN separators
awk 'BEGIN{FS=":"; OFS=","} {print $1, $3, $6}' /etc/passwd