Suppose a log result includes several columns delimited with wild spaces. For some columns like PREEMPTIBLE, EXTERNAL_IP, sometimes they have values, sometime they do not have. See below as an example. In this case, how can I get other columns like INTERNAL_IP?
run: gcloud compute instances list --project=XXX, get:
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
Name1 zone1 type1 true 5.6.7.8 1.2.3.4 RUNNING
Name2 zone1 type1 true 6.5.8.7 RUNNING
Name3 zone2 type2 6.5.8.7 RUNNING
In fact, if all the columns are non-empty, then I can get INTERNAL_IP using:
gcloud compute instances list --project=XXX |tr -s ' ' |awk -F' ' '{if (NR>1)print $5}'
because INTERNAL_IP is the 5th column, or
gcloud compute instances list --project=XXX |tr -s ' ' |awk -F' ' '{if (NR>1)print $(NF-2)}'
because INTERNAL_IP is the 3rd of the last column
But if the columns like PREEMPTIBLE and EXTERNAL_IP have empty string, or sometimes they are completely empty, then it is hard to parse columns like INTERNAL_IP because for some rows INTERNAL_IP is the 5th column, for some rows INTERNAL_IP is the 4th column, etc.
So, ow to get INTERNAL_IP in this case? Thanks.
You can get the index of INTERNAL_IP on the first line and use it to reach internal ip on the following lines:
gcloud compute instances list --project=XXX |\
awk 'NR==1{ii=index($0,"INTERNAL_IP")}NR>1{$0=substr($0,ii);sub(" +.*","");print}'