How to use Regular Expression in AWS CLI Filter










1















I am using AWS Command Line Interface (CLI) to list some AMI Images from AWS.
The Name of an Image is like:



XY_XYZ_Docker_1.13_XYZ_XXYY



When using



aws ec2 describe-images --filters 'Name=name,Values="*_Docker_1.13_*"'


it works as expected.



Now i want to use Regular Expression instead of static value for the Name-Filter.
In the AWS-Docs I read that filtering by RegEx is possible
My approach is:



1:



aws ec2 describe-images --filters 'Name=name,Values="[_]Docker[_][0-9][.][0-9]2[_]"'


The result is always null for this. I tried different ways of quoting the RegEx.



2:



[_]Docker[_][0-9][.][0-9]2[_]


(without quotes) leads to




Error parsing parameter '--filters': Expected: ',', received: 'D' for input:
Name=name,Values=[]Docker[][0-9][.][0-9]2[_]




3:



 *[_]Docker[_][0-9][.][0-9]2[_]*


(with Asterisk) leads to




Error parsing parameter '--filters': Expected: ',', received: ']' for input:
Name=name,Values=[_]Docker[_][0-9][.][0-9]2[_]











share|improve this question






















  • Would you consider piping the output of the AWS CLI to something like grep, and then using that tool's regular expression support?

    – Adil B
    Nov 14 '18 at 15:04











  • I do not want to do that because I have some more filters and the Filter by name needs to be at the beginning

    – uwieuwe4
    Nov 14 '18 at 15:15











  • The AWS CLI uses JMESPath. See: JMESPath Tutorial

    – John Rotenstein
    Nov 15 '18 at 5:56















1















I am using AWS Command Line Interface (CLI) to list some AMI Images from AWS.
The Name of an Image is like:



XY_XYZ_Docker_1.13_XYZ_XXYY



When using



aws ec2 describe-images --filters 'Name=name,Values="*_Docker_1.13_*"'


it works as expected.



Now i want to use Regular Expression instead of static value for the Name-Filter.
In the AWS-Docs I read that filtering by RegEx is possible
My approach is:



1:



aws ec2 describe-images --filters 'Name=name,Values="[_]Docker[_][0-9][.][0-9]2[_]"'


The result is always null for this. I tried different ways of quoting the RegEx.



2:



[_]Docker[_][0-9][.][0-9]2[_]


(without quotes) leads to




Error parsing parameter '--filters': Expected: ',', received: 'D' for input:
Name=name,Values=[]Docker[][0-9][.][0-9]2[_]




3:



 *[_]Docker[_][0-9][.][0-9]2[_]*


(with Asterisk) leads to




Error parsing parameter '--filters': Expected: ',', received: ']' for input:
Name=name,Values=[_]Docker[_][0-9][.][0-9]2[_]











share|improve this question






















  • Would you consider piping the output of the AWS CLI to something like grep, and then using that tool's regular expression support?

    – Adil B
    Nov 14 '18 at 15:04











  • I do not want to do that because I have some more filters and the Filter by name needs to be at the beginning

    – uwieuwe4
    Nov 14 '18 at 15:15











  • The AWS CLI uses JMESPath. See: JMESPath Tutorial

    – John Rotenstein
    Nov 15 '18 at 5:56













1












1








1








I am using AWS Command Line Interface (CLI) to list some AMI Images from AWS.
The Name of an Image is like:



XY_XYZ_Docker_1.13_XYZ_XXYY



When using



aws ec2 describe-images --filters 'Name=name,Values="*_Docker_1.13_*"'


it works as expected.



Now i want to use Regular Expression instead of static value for the Name-Filter.
In the AWS-Docs I read that filtering by RegEx is possible
My approach is:



1:



aws ec2 describe-images --filters 'Name=name,Values="[_]Docker[_][0-9][.][0-9]2[_]"'


The result is always null for this. I tried different ways of quoting the RegEx.



2:



[_]Docker[_][0-9][.][0-9]2[_]


(without quotes) leads to




Error parsing parameter '--filters': Expected: ',', received: 'D' for input:
Name=name,Values=[]Docker[][0-9][.][0-9]2[_]




3:



 *[_]Docker[_][0-9][.][0-9]2[_]*


(with Asterisk) leads to




Error parsing parameter '--filters': Expected: ',', received: ']' for input:
Name=name,Values=[_]Docker[_][0-9][.][0-9]2[_]











share|improve this question














I am using AWS Command Line Interface (CLI) to list some AMI Images from AWS.
The Name of an Image is like:



XY_XYZ_Docker_1.13_XYZ_XXYY



When using



aws ec2 describe-images --filters 'Name=name,Values="*_Docker_1.13_*"'


it works as expected.



Now i want to use Regular Expression instead of static value for the Name-Filter.
In the AWS-Docs I read that filtering by RegEx is possible
My approach is:



1:



aws ec2 describe-images --filters 'Name=name,Values="[_]Docker[_][0-9][.][0-9]2[_]"'


The result is always null for this. I tried different ways of quoting the RegEx.



2:



[_]Docker[_][0-9][.][0-9]2[_]


(without quotes) leads to




Error parsing parameter '--filters': Expected: ',', received: 'D' for input:
Name=name,Values=[]Docker[][0-9][.][0-9]2[_]




3:



 *[_]Docker[_][0-9][.][0-9]2[_]*


(with Asterisk) leads to




Error parsing parameter '--filters': Expected: ',', received: ']' for input:
Name=name,Values=[_]Docker[_][0-9][.][0-9]2[_]








amazon-web-services command-line-interface






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:49









uwieuwe4uwieuwe4

4812




4812












  • Would you consider piping the output of the AWS CLI to something like grep, and then using that tool's regular expression support?

    – Adil B
    Nov 14 '18 at 15:04











  • I do not want to do that because I have some more filters and the Filter by name needs to be at the beginning

    – uwieuwe4
    Nov 14 '18 at 15:15











  • The AWS CLI uses JMESPath. See: JMESPath Tutorial

    – John Rotenstein
    Nov 15 '18 at 5:56

















  • Would you consider piping the output of the AWS CLI to something like grep, and then using that tool's regular expression support?

    – Adil B
    Nov 14 '18 at 15:04











  • I do not want to do that because I have some more filters and the Filter by name needs to be at the beginning

    – uwieuwe4
    Nov 14 '18 at 15:15











  • The AWS CLI uses JMESPath. See: JMESPath Tutorial

    – John Rotenstein
    Nov 15 '18 at 5:56
















Would you consider piping the output of the AWS CLI to something like grep, and then using that tool's regular expression support?

– Adil B
Nov 14 '18 at 15:04





Would you consider piping the output of the AWS CLI to something like grep, and then using that tool's regular expression support?

– Adil B
Nov 14 '18 at 15:04













I do not want to do that because I have some more filters and the Filter by name needs to be at the beginning

– uwieuwe4
Nov 14 '18 at 15:15





I do not want to do that because I have some more filters and the Filter by name needs to be at the beginning

– uwieuwe4
Nov 14 '18 at 15:15













The AWS CLI uses JMESPath. See: JMESPath Tutorial

– John Rotenstein
Nov 15 '18 at 5:56





The AWS CLI uses JMESPath. See: JMESPath Tutorial

– John Rotenstein
Nov 15 '18 at 5:56












1 Answer
1






active

oldest

votes


















0














I wasn't able to find if Jmespath or the --filters flag can support regex, so instead I just piped to Python to run through regex.



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]ockers?[0-9][.][0-9]2", image["Name"])) > 0:
matched_images["Images"].append(image)
print json.dumps(matched_images)
'


You can pipe the output (which is just a JSON string) to your next bash command if needed with a pipe character following the closing quote. Maybe this can address concerns with using grep since it returns a JSON string instead or regular text.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302912%2fhow-to-use-regular-expression-in-aws-cli-filter%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I wasn't able to find if Jmespath or the --filters flag can support regex, so instead I just piped to Python to run through regex.



    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]ockers?[0-9][.][0-9]2", image["Name"])) > 0:
    matched_images["Images"].append(image)
    print json.dumps(matched_images)
    '


    You can pipe the output (which is just a JSON string) to your next bash command if needed with a pipe character following the closing quote. Maybe this can address concerns with using grep since it returns a JSON string instead or regular text.






    share|improve this answer



























      0














      I wasn't able to find if Jmespath or the --filters flag can support regex, so instead I just piped to Python to run through regex.



      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]ockers?[0-9][.][0-9]2", image["Name"])) > 0:
      matched_images["Images"].append(image)
      print json.dumps(matched_images)
      '


      You can pipe the output (which is just a JSON string) to your next bash command if needed with a pipe character following the closing quote. Maybe this can address concerns with using grep since it returns a JSON string instead or regular text.






      share|improve this answer

























        0












        0








        0







        I wasn't able to find if Jmespath or the --filters flag can support regex, so instead I just piped to Python to run through regex.



        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]ockers?[0-9][.][0-9]2", image["Name"])) > 0:
        matched_images["Images"].append(image)
        print json.dumps(matched_images)
        '


        You can pipe the output (which is just a JSON string) to your next bash command if needed with a pipe character following the closing quote. Maybe this can address concerns with using grep since it returns a JSON string instead or regular text.






        share|improve this answer













        I wasn't able to find if Jmespath or the --filters flag can support regex, so instead I just piped to Python to run through regex.



        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]ockers?[0-9][.][0-9]2", image["Name"])) > 0:
        matched_images["Images"].append(image)
        print json.dumps(matched_images)
        '


        You can pipe the output (which is just a JSON string) to your next bash command if needed with a pipe character following the closing quote. Maybe this can address concerns with using grep since it returns a JSON string instead or regular text.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 20:29









        amsgamsg

        1




        1





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302912%2fhow-to-use-regular-expression-in-aws-cli-filter%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo