command line - getting the desirable output with gawk/awk - Ask Ubuntu


i run following command:

aws ec2 describe-instances --filters "name=ip-address,values=my_ip" | grep instanceid  

and get:

"instanceid": "i-b0f13081", 

how can following:

i-b0f13081 

this tried:

 aws ec2 describe-instances --filters "name=ip-address,values=my_ip" | grep instanceid | gawk -f: '{ print $2 }'   "i-b0f13081",  

awk:

set " field delimiter, , 4th field:

% awk -f'"' '{print $4}' <<<'"instanceid": "i-b0f13081",' i-b0f13081 

similarly cut:

% cut -d'"' -f4 <<<'"instanceid": "i-b0f13081",' i-b0f13081 

grep pcre (-p):

% grep -po ':\s*"\k[^"]+' <<<'"instanceid": "i-b0f13081",' i-b0f13081 

shell parameter expansion:

% var='"instanceid": "i-b0f13081",' % var="${var%\"*}" % echo "${var##*\"}" i-b0f13081 

sed:

% sed -e 's/^[^:]+:[^"]+"([^"]+).*/\1/' <<<'"instanceid": "i-b0f13081",' i-b0f13081 

perl:

% perl -pe 's/^[^:]+:[^"]+"([^"]+).*/$1/' <<<'"instanceid": "i-b0f13081",' i-b0f13081 

python:

% python -c 'import sys; print sys.stdin.read().split("\"")[3]' <<<'"instanceid": "i-b0f13081",' i-b0f13081 

Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User

sql server - "Configuration file does not exist", Event ID 274 - Super User