shell scripting — grepping on a column

Dipesh Majumdar
1 min readDec 3, 2020

Suppose you have a bunch of pods running and you want to select only those pods running for 22m (22 minutes). But there is a problem — the string 22m is also there on the first column which you don’t want to consider.

And your normal grep actually doesn’t work here… so you need something else… you need to be able to grep on a specific column.

An example is shown below:

$ k -n somenamespace get poNAME                             READY   STATUS      RESTARTS   AGEdefault-15qg6                    1/1     Running     0          6m45sdefault-6b37w                    1/1     Running     0          23mdefault-c2h25                    1/1     Running     0          75sdefault-f523m                    1/1     Running     0          12mdefault-mf880                    1/1     Running     0          23mdefault-q1sd4                    1/1     Running     0          7m25sdefault-xk03f                    1/1     Running     0          8m25s
$ k -n somenamespace get po |awk -F ' ' '$5 == "23m"'
default-6b37w 1/1 Running 0 23mdefault-mf880 1/1 Running 0 23m

As you can see in the above output the pod: default-f523m was not selected in the output, and that’s exactly what we wanted. :-)

but better than this is this method:

so in the xyz.redirects-entertainment.map file I have got several redirects like this —

multiple rows and 2 coluns separated by space.

xyz/entertainment/blah pqr/somethingelse/blah-blah

abc/sports/blah nst/blah-more-blah

….

…….

Now i m only interested to filter 1st column where the string entertainment is present, so this is how i do it.

cat xyz.redirects-entertainment.map |awk -F' ' '$1 ~ /entertainment/'

--

--