0

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
mdailey77
  • 1,673
  • 4
  • 26
  • 52
  • Possible duplicate of https://stackoverflow.com/questions/74071913/split-a-json-string-value-using-jmespath – β.εηοιτ.βε Dec 07 '22 at 14:45
  • I've seen an example using a Linux cut command like this: `aws sts get-caller-identity --output text --query 'Arn' | cut -d\"/\" -f2`. Couldn't I do something using Powershell syntax? – mdailey77 Dec 07 '22 at 15:06

1 Answers1

0

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
Paolo
  • 21,270
  • 6
  • 38
  • 69
  • Thanks. The native command worked. Using `jq` didn't work. – mdailey77 Dec 07 '22 at 18:01
  • @mdailey77 well, "didn't work" doesn't tell me much. What error do you see when using the `jq` solution? – Paolo Dec 07 '22 at 18:05
  • This is the error I got: `jq: The term 'jq' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.` – mdailey77 Dec 07 '22 at 19:05
  • @mdailey77 that means `jq` isn't installed – Paolo Dec 07 '22 at 19:22