skip to Main Content

I have this command which displays all the record from the search. But I would like to filter only the record that matches the search word.

For e.g.

for user in $(aws iam list-users |grep -i UserName|sed -e 's/.*: "//' -e 's/",//'); do 
    echo USER: $user; 
    echo TAGS:
    aws iam list-user-tags --user-name $user --output text | awk '{print $2,$3}'
    echo GROUPS:
    aws iam list-groups-for-user  --user-name $user --output text|awk {'print $5'};  done > users.txt

The above command displays the following results.

User: [email protected]
TAGS:
Team red
Status active
Environment: nonprod
GROUPS:
iam-nonprod
iam-prod

User: [email protected]
TAGS:
Team green
Status active
Environment: nonprod
GROUPS:
iam-nonprod
iam-prod

etc.

I would like get all the user where tag Team == red.

I tried with search string in line 4 like,

aws iam list-user-tags --user-name $user --output text | awk '/red/{print $2,$3}'

but it displays only one line

Team red

But I would like to display full record like

User: [email protected]
TAGS:
Team red
Status active
Environment: nonprod
GROUPS:
iam-nonprod
iam-prod

Could you please help how I can display all the record where tag Team == red.

3

Answers


  1. For awk, you can use the paragraph mode. This will display all "records" that contain Team red.

    awk -v RS= '/Team red/'
    
    Login or Signup to reply.
  2. You can solve this with various awscli commands and the use of the --query option which allows you to perform conditional client-side filtering.

    Here is an example:

    #!/bin/bash
    
    USERS=$(aws iam list-users --query "Users[*].UserName" --output text)
    
    for user in $USERS; do
        TAG=$(aws iam list-user-tags --user-name $user --query 'Tags[?(Key==`Team` && Value==`red`)]' --output text)
    
        if [ "$TAG" != "" ]; then
            echo "User:" $user
    
            echo "Tags:"
            aws iam list-user-tags --user-name $user --query 'Tags[*].[Key,Value]' --output text | tr "t" "="
    
            echo "Groups:"
            aws iam list-groups-for-user --user-name $user --query "Groups[*].GroupName" --output text | tr "t" "n"
        fi
    done
    

    Sample output:

    User: jason
    Tags:
    Team=red
    Role=development
    Groups:
    dev
    User: mary
    Tags:
    Team=red
    Role=test
    Groups:
    qa
    ut
    fv
    
    Login or Signup to reply.
  3. It’s super easy with AWK. First put your data in a file and this command will do whole job:

    awk '/Team red/{c=4} c-->-2' < file
    
    $ cat myfile
    User: [email protected]
    TAGS:
    Team red
    Status active
    Environment: nonprod
    GROUPS:
    iam-nonprod
    iam-prod
    
    User: [email protected]
    TAGS:
    Team green
    Status active
    Environment: nonprod
    GROUPS:
    iam-nonprod
    iam-prod
    
    etc.
    $ awk '/Team red/{c=4} c-->-2' < file
    User: [email protected]
    TAGS:
    Team red
    Status active
    Environment: nonprod
    GROUPS:
    iam-nonprod
    iam-prod
    [brhosh@scp-3-scripting(enm2) test]$ 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search