0

I'm trying to make a simple filtering script using AWS cli + jq (powershell or cmd in Windows).

aws iam list-roles | jq -c '.Roles[].RoleName | select(startswith ("blabla"))'

But getting this error:

jq: error: AD_/0 is not defined at <top-level>, line 1:
.Roles[].RoleName | select(startswith (AD_))
jq: 1 compile error

But using jqplay.org with same JSON everything works well. Any thoughts?

thanks!

aws iam list-roles | jq -c '.Roles[].RoleName | select(startswith ("blabla"))'

blabla_rolename_1

  • Please share the output of `aws iam list-roles` or the jqplay link to include a [mre]. – 0stone0 Dec 20 '22 at 14:20
  • `select(startswith (AD_))` is missing the quotes: `select(startswith ("AD_"))`. However that is not the same filter as you state in your question – 0stone0 Dec 20 '22 at 14:21
  • Your code block and your error message do not match – knittl Dec 20 '22 at 19:36
  • I think this might be helpful (although frustrating): https://stackoverflow.com/questions/6714165/powershell-stripping-double-quotes-from-command-line-arguments – Weeble Dec 21 '22 at 02:18

2 Answers2

0

It looks like you might be missing a string after the startswith function. Try adding a string in quotes after startswith:

aws iam list-roles | jq -c '.Roles[].RoleName | select(startswith("blabla"))'

If you still get an error, you can try running the jq command without the -c flag to see the full output from jq, which might give you more information about the error.

aws iam list-roles | jq '.Roles[].RoleName | select(startswith("blabla"))
Flori
  • 2,453
  • 1
  • 21
  • 31
0

AFAIK Powershell does not support single quotes. Use double quotes and escaped double quotes instead:

aws iam list-roles | jq -c ".Roles[].RoleName | select(startswith (\"blabla\"))"
knittl
  • 246,190
  • 53
  • 318
  • 364