How to get the third row from STDOUT?

6/16/2020

I have a STDOUT in form of table with the following content:

$kubectl get postgresql

NAME                   TEAM        VERSION   PODS   VOLUME   CPU-REQUEST   MEMORY-REQUEST   AGE     STATUS
acid-minimal-cluster   acid        12        2      1Gi                                     2d16h   SyncFailed
acid-userdb            acid        12        2      5Gi      100m          100Mi            2d16h   SyncFailed
databaker-userdb       databaker   12        2      2Gi      100m          100Mi            2d16h   SyncFailed
databaker-users-db     databaker   12        2      2Gi      100m          100Mi            2d15h   Running

and would like to get the row with NAME = *users-db and the STATUS = Running, that means the last line.

What I am trying achieve is, to check if the database has been successfully deployed or not.

Before this coding block:

echo "Check if database $DB_NAME is correctly deployed"
for i in {1..3}; do
  if [ kubectl get postgresql $DB_NAME | egrep -q "$DB_NAME|Running" ] ; then
    echo "DB $DB_NAME successfully deployed"
    break
  elif [ $i -eq 3 ]; then
    echo "DB $DB_NAME not successfully deployed"
    exit 1
  fi
  sleep 3
done

I have to pick up the right row first.

How to write a bash script to get the corresponding row with NAME = *users-db and the STATUS = Running ?

-- softshipper
bash
kubernetes
postgresql

2 Answers

6/16/2020

Try

awk '$1~/users-db$/ && $9=="Running"'

$1 < -- First field
$9 < -- Last field
\ \ <-- Used for regex match

-- Digvijay S
Source: StackOverflow

6/16/2020
kubectl get postgresql | sed -n '/databaker-userdb/p'

kubectl get postgresql | sed -n '3p'

The first option is probably safer with modern datafiles which don't tend to have consistent structures, because it uses a keyword. If you always know which line number you want, however, you can just print that.

-- petrus4
Source: StackOverflow