如何修改jq正则匹配规则,使AWS SecretsManager查询仅返回目标密钥?
解决方案
你的问题出在正则表达式的写法上:match("example-*")中的*是正则量词,代表匹配前面的-零次或多次,所以examplecompany-secret_key里的example后面没有-也会被匹配到。
根据你的需求,分两种情况修改命令:
只返回 example-secret_key
如果要精准匹配以example-开头的密钥名称,有两种方式:
方式1:修正正则表达式
使用^表示字符串开头,确保只匹配以example-开头的项:
aws secretsmanager list-secrets | jq -r '.SecretList[] | select(.Name | match("^example-")) | .Name'
方式2:使用startswith函数(更直观)
直接用jq的字符串前缀判断函数,无需正则:
aws secretsmanager list-secrets | jq -r '.SecretList[] | select(.Name | startswith("example-")) | .Name'
只返回 examplecompany-secret_key
同理,精准匹配以examplecompany-开头的项:
aws secretsmanager list-secrets | jq -r '.SecretList[] | select(.Name | match("^examplecompany-")) | .Name'
或者用startswith:
aws secretsmanager list-secrets | jq -r '.SecretList[] | select(.Name | startswith("examplecompany-")) | .Name'
内容的提问来源于stack exchange,提问作者pi.
相关产品推荐
相关产品推荐

