Interview Prep/Real-World Interview/awk, sed, grep and jq one-liners

Using jq, group a list of pods by node and count them.

AdvancedHands-onawk, sed, grep and jq one-liners
Answer
kubectl get pods -A -o json | jq -r '
  [.items[] | {node: (.spec.nodeName // "unscheduled")}]
  | group_by(.node) | map({node: .[0].node, count: length})
  | sort_by(-.count)[] | "\(.count)\t\(.node)"'

Key trick → group_by needs a key and returns arrays. // is jq's "alternative" operator for null values (Pending pods have no nodeName).

All Real-World Interview questions Roadmap: Interview readiness

More on awk, sed, grep and jq one-liners