Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Monday, August 10, 2020

Show count and instance IDs of existing EC2 instances in all regions of AWS:

I'm kicking myself for not posting this earlier in my past life-- but here's a handy shell script that can list the number of instances in each region in AWS.


Show count and instance IDs of existing EC2 instances in all regions of AWS:


Single line:

Set my AWS profile to my personal account rather than work.  
To setup, use aws --profile personal configure   

$ export aws_profile=personal


If the buffer space can handle long entries, you can run this in a single line, otherwise there's a multi line below to run as a script.

    $ for i in $(aws --profile $aws_profile ec2 describe-regions --output json | grep "RegionName" | cut -d '"' -f4 | sort); do export buf=$(aws --profile $aws_profile ec2 --region $i describe-instances --output json | grep InstanceId | cut -d '"' -f4); export buf_num=$(echo $buf | grep -c .); echo "=== $i -> $buf_num instance(s) ===";echo $buf ; done

    Multi Line:

    Good for placing into a script to run, also listed this way for readability:
    1. #!/bin/bash export aws_profile=personal for i in $(aws --profile $aws_profile ec2 describe-regions --output json| grep "RegionName" | cut -d '"' -f4 | sort); do export buf=$(aws ec2 describe-instances \ --profile $aws_profile \ --region $i \ --output json | \ grep InstanceId | \ cut -d '"' -f4); export buf_num=$(echo $buf | grep -c .); echo "=== $i -> $buf_num instance(s) ==="; echo $buf; done

    Sample Output:

    1. === ap-northeast-1 -> 0 instances === === ap-northeast-2 -> 0 instances === === ap-south-1 -> 0 instances === === ap-southeast-1 -> 0 instances === === ap-southeast-2 -> 0 instances === === ca-central-1 -> 0 instances === === eu-central-1 -> 0 instances === === eu-north-1 -> 0 instances === === eu-west-1 -> 0 instances === === eu-west-2 -> 0 instances === === eu-west-3 -> 0 instances === === sa-east-1 -> 0 instances === === us-east-1 -> 0 instances === === us-east-2 -> 0 instances === === us-west-1 -> 0 instances === === us-west-2 -> 1 instances === i-012345678901234ab

    Tuesday, July 8, 2014

    Multiply / Sum column data in awk/sed

    Sourced from here:
    http://unix.stackexchange.com/questions/115998/how-do-i-multiply-and-sum-column-data-using-awk-and-or-sed

    I have several columns of data. I always have the same number of rows (say 5). In the 2nd column, I want to multipy the first value by 5, then the second value by 4, the third value by 3, etc. I then want to sum these values, and divide by the sum of the values in the second column. How would I do this in sed and/or awk?
    Example:
    4 5 7 1 2 3
    5 1 2 3 1 2
    4 2 3 6 1 2
    3 4 1 6 3 3
    2 3 1 2 1 6
    Answer: (5*5 + 4*1 + 3*2 + 2*4 + 1*3)/(5 + 1 + 2 + 4 + 3) = 3.067


    Solution 1:
    Replace 6 with total (number of lines + 1) if needed:
    awk '{mult+=$2*(6-NR); sum+=$2;} END {print mult/sum;}' yourfile.txt 
    Displays: 3.06667


    Solution 2:
    If in case total number of lines are not known
    $ cat file
    4 5 7 1 2 3
    5 1 2 3 1 2
    4 2 3 6 1 2
    3 4 1 6 3 3
    2 3 1 2 1 6
    
    $ awk 'FNR==NR{t = NR+1;next}{mult+=$2*(t-FNR);sum+=$2} 
           END{print mult/sum}' file{,}
    3.06667
    Explanation
    awk 'FNR == NR{t=NR+1;next}
    FNR variable --> Current line number in the current file
    NR variable-->The total number of lines seen so far
    The condition FNR == NR can only be true while awk is reading the first file of argument
    t=NR+1 --> variable t holds line count +1 extra since it's required in our current context
    next--> Stops the processing of the current input record and proceeds with the next input record
    mult+=$2*(t-FNR) --> variable mult holds some of (5*(t-FNR) + ..... + 4*1 + 3*2 + 2*4 + 1*3)
    sum+=$2 --> variable sum holds sum of column2
    END--> AnEND rule is executed, once, after all the input has been read
    print mult/sum --> Finally print mult/sum
    file{,} --> you can also file file file but i used shortcut this bash trick you can say, try echo file{,} and echo file{,,} on your terminal.