I can’t believe I spent all these years using only grep.
Most of us know how to use awk
to print the nth column of a file:
$ awk '{print $1}' /etc/hosts
will print all IP addresses from /etc/hosts
But you can also do filtering before printing the chosen column:
$ awk '$5 >= 2 {print $2}' /path/to/file
will print the second column of all lines, where the 5th column is greater than 2. That would have been hard with grep.
Now I can use that to find out all deployments on my openshift cluster, where the number of current replicas is greater than 2.
$ oc get deployments --all-namespaces | awk '$5 >= 2 {print $2}'
NAME
oauth-openshift
console
downloads
router-default
etcd-quorum-guard
prometheus-adapter
thanos-querier
packageserver
I know that openshift/kubernetes both have a powerful query selector syntax, but for the moment awk
will do.