我正在使用AWS Command Line Interface(CLI)列出来自AWS的一些AMI映像。 图片名称如下:
XY_XYZ_Docker_1.13_XYZ_XXYY
使用
aws ec2 describe-images --filters 'Name=name,Values="*_Docker_1.13_*"'
它按预期工作。
现在,我想对名称过滤器使用正则表达式而不是静态值。 在AWS文档中,我阅读了RegEx is possible进行的过滤 我的方法是:
1:
aws ec2 describe-images --filters 'Name=name,Values="[_]Docker[_][0-9][.][0-9]{2}[_]"'
此结果始终为 null 。我尝试了不同的方式引用RegEx。
2:
[_]Docker[_][0-9][.][0-9]{2}[_]
(不带引号)导致
解析参数'--filters'时出错:预期:',',收到:输入的'D': 名称=名称,值= [] Docker [] [0-9] [。] [0-9] {2} [_]
3:
*[_]Docker[_][0-9][.][0-9]{2}[_]*
(带有星号)导致
错误解析参数'--filters':预期:',',收到:']'作为输入: 名称=名称,值= [_] Docker [_] [0-9] [。] [0-9] {2} [_]
答案 0 :(得分:0)
我找不到Jmespath或--filters标志是否可以支持正则表达式,所以我只是通过管道将其传递给Python以通过正则表达式运行。
aws ec2 describe-images --filters 'Name=name,Values="*Docker*"' | \
python -c '
import json, sys, re
obj = json.load(sys.stdin)
matched_images = {"Images":[]}
for image in obj["Images"]:
if len(re.findall(r"[Dd]ocker\s?[0-9][.][0-9]{2}", image["Name"])) > 0:
matched_images["Images"].append(image)
print json.dumps(matched_images)
'
如果需要,可以将输出(只是一个JSON字符串)通过管道传递到下一个bash命令,并在结束引号后加上管道字符。也许这可以解决使用grep的问题,因为它返回的是JSON字符串或常规文本。