Check if string is present in usernames in multiple AWS accounts using awscli and jq

for profile in $(grep "\[" ~/.aws/credentials | tr -d '[|]'); \
do echo $profile; \
aws --profile $profile iam list-users | jq '.Users[] | select(.UserName | contains("dmitri"))'; \
This will:
  • get all AWS profiles from awscli config ~/.aws/credentials
  • filter the profiles, by removing symbols [ and ] with tr
  • print the profile name
  • list all users in the AWS profile using awscli tool - an output will be a json
  • filter the json by UserName field with jq which will contain the string dmitri and print the result