Linux Tools — kubectl Output Processing Reference¶
See also:
k8s commands combined with linux.mdfor the full pipeline pattern with kubectl.
The Core 8 Tools¶
Every one of these shows up constantly when manipulating kubectl output. They're composable — chain them with |.
| Tool | What it does | Example |
|---|---|---|
awk '{print $N}' |
Extract column N (whitespace-separated) | awk '{print $1}' = first column |
sort |
Sort ascending (alphabetical or by line) | sort = A-Z |
sort -r |
Sort descending | sort -r = Z-A |
sort -n |
Sort numerically (not alphabetically) | sort -n for numbers |
tail -n +N |
Skip first N-1 lines (start from line N) | tail -n +2 = skip header row |
head -n N |
Take first N lines | head -1 = top result only |
grep "pattern" |
Keep lines matching pattern | grep Running |
grep -v "pattern" |
Drop lines matching pattern | grep -v Completed |
wc -l |
Count lines | wc -l = number of pods |
cut -d',' -f1 |
Cut by delimiter, take field N | cut -d',' -f2 = 2nd CSV field |
uniq |
Remove consecutive duplicate lines | Always sort first or it won't work |
tee file.txt |
Write to file AND pass through to stdout | Capture and continue pipeline |
sort Variants¶
sort # alphabetical ascending
sort -r # alphabetical descending
sort -n # numeric ascending (1, 2, 10 — not 1, 10, 2)
sort -rn # numeric descending
sort -u # sort + deduplicate (same as sort | uniq)
sort -k2 # sort by column 2 (whitespace-separated)
sort -k2 -n # sort by column 2 numerically
sort -t',' -k2 # sort by column 2 using comma as delimiter
Why sort before uniq: uniq only removes consecutive duplicates. If duplicates are scattered through the input, uniq misses them. sort first groups all duplicates together so uniq can catch them.
tail -n +N — The Header Skipper¶
The +N syntax means "start from line N". Standard tail -N means "last N lines". These are opposite behaviours — + changes the meaning.
Most common use:
awk — Column Extraction¶
awk '{print $1}' # first column
awk '{print $6}' # 6th column (IP in kubectl get pods -o wide)
awk '{print $1,$6}' # columns 1 and 6, space-separated
awk '{print $1","$6}' # columns 1 and 6, comma-separated
awk '{print $1"/"$2}' # namespace/podname format
Why awk over cut: kubectl output uses variable-width spaces between columns. cut -d' ' breaks with multiple spaces. awk splits on any whitespace — handles kubectl's format correctly.
tee — Write to File AND Continue¶
kubectl get pods | tee pods.txt | grep Running
# ↑ writes all pods to file, passes ALL through to grep
vs > which terminates the pipeline:
Use tee when you want to save the raw output AND continue processing it. > when you just want to save.
Practical Patterns with kubectl¶
# Get pod IPs sorted, with header
echo "IP_ADDRESS" > pod_ips.txt
kubectl get pods -o wide | tail -n +2 | awk '{print $6}' | sort >> pod_ips.txt
# Count running pods
kubectl get pods | grep Running | wc -l
# Get pod names only (no headers)
kubectl get pods -o wide | tail -n +2 | awk '{print $1}'
# Pods on a specific node
kubectl get pods -o wide | grep node01 | awk '{print $1}'
# Top CPU pod - name and namespace
kubectl top pod -A --sort-by=cpu --no-headers | head -1 | awk '{print $1"/"$2}'
# Nodes sorted by memory usage (node_resource.md pattern)
kubectl top node --sort-by=memory --no-headers | head -1 | awk '{print $1}'
# All unique images running in a namespace
kubectl get pods -o wide | tail -n +2 | awk '{print $7}' | sort -u
Column Positions — Quick Reference¶
kubectl get pods -o wide:
| $N | Content |
|---|---|
| $1 | NAME |
| $2 | READY |
| $3 | STATUS |
| $4 | RESTARTS |
| $5 | AGE |
| $6 | IP |
| $7 | NODE |
kubectl get nodes -o wide:
| $N | Content |
|---|---|
| $1 | NAME |
| $2 | STATUS |
| $3 | ROLES |
| $4 | AGE |
| $5 | VERSION |
| $6 | INTERNAL-IP |
kubectl top pod -A --no-headers (with -A namespace column shifts everything):
| $N | Content |
|---|---|
| $1 | NAMESPACE |
| $2 | NAME |
| $3 | CPU |
| $4 | MEMORY |
kubectl top pod --no-headers (no -A):
| $N | Content |
|---|---|
| $1 | NAME |
| $2 | CPU |
| $3 | MEMORY |