Home > Net >  select part of AWS CLI query output
select part of AWS CLI query output

Time:12-08

I want to return only the current AWS username using AWS CLI. I'm on Windows 11. I think there's a way to do it using a regex but I can't figure out how. I think I need to use a pipe along with a regex but there's no related examples on the JMESPath website. I want to have something like "only return the text after 'user/' ".

Here's what I have so far:

aws sts get-caller-identity --output text --query 'Arn'

which returns `"arn:aws:iam::999999009999:user/joe.smith"

I just want to return "joe.smith".

CodePudding user response:

jmes does not support splitting a string nor matching a substring in a string, so you'll have to resort to a native command like:

> (aws sts get-caller-identity --output text --query 'Arn').Split("/")[-1]

or use something like jq :

$ aws sts get-caller-identity --output json | jq '.Arn | split("/")[-1]' -r
  • Related