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
Post a Comment