-   Linux - Newbie (
-   -   need help fetching required information ( 05-30-2012 12:20 AM

need help fetching required information
I call a sh file it returns lot of information I wan to retrieve a part of it

Here is what I get

[ec2-user@domU-12-31-39-10-44-32 ~]$ /opt/aws/apitools/ec2/bin/ec2-describe-instances --filter "instance-id=i-8d28b5eb"
RESERVATION    r-61b4d103      424912172560    artms-web
INSTANCE        i-8d28b5eb      ami-e565ba8c      domU-12-31-39-10-44-32.compute-1.internal      running artms-default  0
                m1.large        2012-05-25T18:01:29+0000        us-east-1a      aki-88aa75e1                    monitoring-enabled  e
bs                                      paravirtual    xen            sg-e55aeb8d    default
BLOCKDEVICE    /dev/sda1      vol-84242deb    2012-05-25T18:01:57.000Z        true
BLOCKDEVICE    /dev/sdf        vol-04ffe76b    2012-05-25T18:01:57.000Z        false
TAG    instance        i-8d28b5eb      Name    stg-app/web-0

all I want from this is value of Name which is stg-app/web-0
also I want to check if the value of name which is stg-app/web-0 contains "stg"

please help me.

jhwilliams 05-30-2012 02:44 AM

That's easy, just pipe into awk:


/opt/aws/apitools/ec2/bin/ec2-describe-instances \
    --filter "instance-id=i-8d28b5eb" | \
    awk '/Name/ { print $NF }'

To check if it contains stg, just add another pipe to grep -c.


stg=$(/opt/aws/apitools/ec2/bin/ec2-describe-instances \
    --filter "instance-id=i-8d28b5eb" | \
    awk '/Name/ { print $NF }' | \
    grep -c 'stg')

if [ $stg -gt 0 ]; then
    echo "It contained stg."
    echo "No stg."

pan64 05-30-2012 02:57 AM


Originally Posted by jhwilliams (Post 4690932)
To check if it contains stg, just add another pipe to grep -c.

Oh, no!
if you already caught that string you can check it in awk, do not use grep:

awk '/Name/ { print $NF; if ( index($NF, "stg") ) print "STG" }'

All times are GMT -5. The time now is 08:48 PM.