LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need help fetching required information (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-fetching-required-information-947503/)

fachhoch@gmail.com 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
Code:

[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    ec2-23-22-144-254.compute-1.amazonaws.com      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      23.22.144.254  10.198.71.192  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:

Code:

/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.

Code:

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."
else
    echo "No stg."
fi


pan64 05-30-2012 02:57 AM

Quote:

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:
Code:

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


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