Reputation: 8961
my question concerning a linux output :
using gcloud
to list my vm instances :
HOSTNAME="chef-production2-doctor-simulator01"
gcloud compute instances list |grep $HOSTNAME
chef-production2-doctor-simulator01 us-central1-b n1-standard-1 10.128.0.2 x.x.x.x RUNNING
how can I grep the internal ip and output it to a bash variable?
INTERNAL_IP="$(gcloud compute instances list |grep $HOSTNAME)"
Upvotes: 1
Views: 315
Reputation: 783
You could try it like thie:
var INTERNAL_IP="$(gcloud compute instances list | grep chef-production2-doctor-simulator01 | tr -s " " | cut -d ' ' -f4)"
The tr -s " " deletes all double spaces The cut -d ' ' -f4 cuts out the ip
or as anony mentioned (which is better):
var INTERNAL_IP="$(gcloud compute instances list | awk '/chef-production2-doctor-simulator01/ { print $4 }'")
Upvotes: 0
Reputation: 77454
The popular tools for selecting a column are: cut
, sed
and awk
. And of course, perl
.
awk
often is the shortest solution, but you will have to learn it's syntax.
gcloud compute instances list | \
awk '/chef-production2-doctor-simulator01/ { print $2 }'
may be what you are looking for. You may need to be explicit about separators. It's hard to tell which separators were used in the example you showed above.
Upvotes: 2