Merge two greps into single one





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







3















I need to extract an ID from the output of another command. Currently my extracting command looks like:



someID=$(command | grep -oP '(?:^Successfullysbuilts)([da-z]{12}$)' | grep -oP '([a-zd]{12})')


Example command output:



 ---> Using cache
---> 9b4624927fa6
Successfully built 9b4624927fa6


Expected result:



9b4624927fa6


ID extracted from line



Successfully built 9b4624927fa6


How could I merge those two grep statements into single one?










share|improve this question









New contributor




stetoc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • I don't really understand your question. Your command seems to do exactly what you want.

    – Sjoerd
    Apr 11 at 8:12






  • 6





    If you are building a docker image, you can give it a name with the --tag option. That way, you can supply the name instead of having to parse it from the output. Also, --quiet supresses all output except for the image ID.

    – Sjoerd
    Apr 11 at 8:13











  • @Sjoerd Yes, but they want to combine the two grep calls into a single command.

    – Kusalananda
    Apr 11 at 8:16











  • @Sjoerd oh thanks for suggestion, will definitely try it, I need that id exactly for this reason, want to build simple automated pipeline for docker containers

    – stetoc
    Apr 11 at 8:17


















3















I need to extract an ID from the output of another command. Currently my extracting command looks like:



someID=$(command | grep -oP '(?:^Successfullysbuilts)([da-z]{12}$)' | grep -oP '([a-zd]{12})')


Example command output:



 ---> Using cache
---> 9b4624927fa6
Successfully built 9b4624927fa6


Expected result:



9b4624927fa6


ID extracted from line



Successfully built 9b4624927fa6


How could I merge those two grep statements into single one?










share|improve this question









New contributor




stetoc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • I don't really understand your question. Your command seems to do exactly what you want.

    – Sjoerd
    Apr 11 at 8:12






  • 6





    If you are building a docker image, you can give it a name with the --tag option. That way, you can supply the name instead of having to parse it from the output. Also, --quiet supresses all output except for the image ID.

    – Sjoerd
    Apr 11 at 8:13











  • @Sjoerd Yes, but they want to combine the two grep calls into a single command.

    – Kusalananda
    Apr 11 at 8:16











  • @Sjoerd oh thanks for suggestion, will definitely try it, I need that id exactly for this reason, want to build simple automated pipeline for docker containers

    – stetoc
    Apr 11 at 8:17














3












3








3








I need to extract an ID from the output of another command. Currently my extracting command looks like:



someID=$(command | grep -oP '(?:^Successfullysbuilts)([da-z]{12}$)' | grep -oP '([a-zd]{12})')


Example command output:



 ---> Using cache
---> 9b4624927fa6
Successfully built 9b4624927fa6


Expected result:



9b4624927fa6


ID extracted from line



Successfully built 9b4624927fa6


How could I merge those two grep statements into single one?










share|improve this question









New contributor




stetoc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I need to extract an ID from the output of another command. Currently my extracting command looks like:



someID=$(command | grep -oP '(?:^Successfullysbuilts)([da-z]{12}$)' | grep -oP '([a-zd]{12})')


Example command output:



 ---> Using cache
---> 9b4624927fa6
Successfully built 9b4624927fa6


Expected result:



9b4624927fa6


ID extracted from line



Successfully built 9b4624927fa6


How could I merge those two grep statements into single one?







grep regular-expression






share|improve this question









New contributor




stetoc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




stetoc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Apr 12 at 1:02









John Kugelman

1,48011020




1,48011020






New contributor




stetoc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 11 at 8:05









stetocstetoc

1184




1184




New contributor




stetoc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





stetoc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






stetoc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • I don't really understand your question. Your command seems to do exactly what you want.

    – Sjoerd
    Apr 11 at 8:12






  • 6





    If you are building a docker image, you can give it a name with the --tag option. That way, you can supply the name instead of having to parse it from the output. Also, --quiet supresses all output except for the image ID.

    – Sjoerd
    Apr 11 at 8:13











  • @Sjoerd Yes, but they want to combine the two grep calls into a single command.

    – Kusalananda
    Apr 11 at 8:16











  • @Sjoerd oh thanks for suggestion, will definitely try it, I need that id exactly for this reason, want to build simple automated pipeline for docker containers

    – stetoc
    Apr 11 at 8:17



















  • I don't really understand your question. Your command seems to do exactly what you want.

    – Sjoerd
    Apr 11 at 8:12






  • 6





    If you are building a docker image, you can give it a name with the --tag option. That way, you can supply the name instead of having to parse it from the output. Also, --quiet supresses all output except for the image ID.

    – Sjoerd
    Apr 11 at 8:13











  • @Sjoerd Yes, but they want to combine the two grep calls into a single command.

    – Kusalananda
    Apr 11 at 8:16











  • @Sjoerd oh thanks for suggestion, will definitely try it, I need that id exactly for this reason, want to build simple automated pipeline for docker containers

    – stetoc
    Apr 11 at 8:17

















I don't really understand your question. Your command seems to do exactly what you want.

– Sjoerd
Apr 11 at 8:12





I don't really understand your question. Your command seems to do exactly what you want.

– Sjoerd
Apr 11 at 8:12




6




6





If you are building a docker image, you can give it a name with the --tag option. That way, you can supply the name instead of having to parse it from the output. Also, --quiet supresses all output except for the image ID.

– Sjoerd
Apr 11 at 8:13





If you are building a docker image, you can give it a name with the --tag option. That way, you can supply the name instead of having to parse it from the output. Also, --quiet supresses all output except for the image ID.

– Sjoerd
Apr 11 at 8:13













@Sjoerd Yes, but they want to combine the two grep calls into a single command.

– Kusalananda
Apr 11 at 8:16





@Sjoerd Yes, but they want to combine the two grep calls into a single command.

– Kusalananda
Apr 11 at 8:16













@Sjoerd oh thanks for suggestion, will definitely try it, I need that id exactly for this reason, want to build simple automated pipeline for docker containers

– stetoc
Apr 11 at 8:17





@Sjoerd oh thanks for suggestion, will definitely try it, I need that id exactly for this reason, want to build simple automated pipeline for docker containers

– stetoc
Apr 11 at 8:17










2 Answers
2






active

oldest

votes


















10














A slight modification of your first grep works for me:



$ grep -oP '^SuccessfullysbuiltsK[da-z]{12}$' example-output
9b4624927fa6


K in PCRE resets the match start:




The escape sequence K causes any previously matched characters not to
be included in the final matched sequence.




It's similar to a zero-width positive look-behind assertion (?<=Successfully...).






share|improve this answer





















  • 4





    K is not a look behind assertion, it just sets the start of the matched text here reported by -o. Compare echo aaaaa | grep -Po 'aK.' with echo aaaaa | grep -Po '(?<=a).' (this time using a look-behind assertion).

    – Stéphane Chazelas
    Apr 11 at 16:17






  • 1





    @StéphaneChazelas: To be fair, perlre describes it in the same list item as positive look-behind assertions, with language and typography that strongly suggests thinking of one as a variant of the other (even though that can't be the case at a technical level).

    – Henning Makholm
    Apr 11 at 22:48






  • 1





    @HenningMakholm, yes, it's unfortunate. The pcrepattern man page does not make that confusion. It's true K can often be used in place or positive look behind assertions.

    – Stéphane Chazelas
    Apr 11 at 22:58











  • @StéphaneChazelas thanks, corrected.

    – muru
    Apr 12 at 6:14



















7














To get the hexadecimal number at the end of a line that starts with Successfully built, I would probably use sed:



sed -n -E 's/^Successfully built ([[:xdigit:]]+)$/1/p'


This replaces the matching line with the hash and prints it (and no other line).



Or awk:



awk '/^Successfully built [[:xdigit:]]+$/ { print $NF }'


This prints the last whitespace-delimited field of each line matching the regular expression.



In both regular expressions, the [[:xdigit:]]+ expression will match a non-empty string of hexadecimal digits.






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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
    });


    }
    });






    stetoc is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511837%2fmerge-two-greps-into-single-one%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    10














    A slight modification of your first grep works for me:



    $ grep -oP '^SuccessfullysbuiltsK[da-z]{12}$' example-output
    9b4624927fa6


    K in PCRE resets the match start:




    The escape sequence K causes any previously matched characters not to
    be included in the final matched sequence.




    It's similar to a zero-width positive look-behind assertion (?<=Successfully...).






    share|improve this answer





















    • 4





      K is not a look behind assertion, it just sets the start of the matched text here reported by -o. Compare echo aaaaa | grep -Po 'aK.' with echo aaaaa | grep -Po '(?<=a).' (this time using a look-behind assertion).

      – Stéphane Chazelas
      Apr 11 at 16:17






    • 1





      @StéphaneChazelas: To be fair, perlre describes it in the same list item as positive look-behind assertions, with language and typography that strongly suggests thinking of one as a variant of the other (even though that can't be the case at a technical level).

      – Henning Makholm
      Apr 11 at 22:48






    • 1





      @HenningMakholm, yes, it's unfortunate. The pcrepattern man page does not make that confusion. It's true K can often be used in place or positive look behind assertions.

      – Stéphane Chazelas
      Apr 11 at 22:58











    • @StéphaneChazelas thanks, corrected.

      – muru
      Apr 12 at 6:14
















    10














    A slight modification of your first grep works for me:



    $ grep -oP '^SuccessfullysbuiltsK[da-z]{12}$' example-output
    9b4624927fa6


    K in PCRE resets the match start:




    The escape sequence K causes any previously matched characters not to
    be included in the final matched sequence.




    It's similar to a zero-width positive look-behind assertion (?<=Successfully...).






    share|improve this answer





















    • 4





      K is not a look behind assertion, it just sets the start of the matched text here reported by -o. Compare echo aaaaa | grep -Po 'aK.' with echo aaaaa | grep -Po '(?<=a).' (this time using a look-behind assertion).

      – Stéphane Chazelas
      Apr 11 at 16:17






    • 1





      @StéphaneChazelas: To be fair, perlre describes it in the same list item as positive look-behind assertions, with language and typography that strongly suggests thinking of one as a variant of the other (even though that can't be the case at a technical level).

      – Henning Makholm
      Apr 11 at 22:48






    • 1





      @HenningMakholm, yes, it's unfortunate. The pcrepattern man page does not make that confusion. It's true K can often be used in place or positive look behind assertions.

      – Stéphane Chazelas
      Apr 11 at 22:58











    • @StéphaneChazelas thanks, corrected.

      – muru
      Apr 12 at 6:14














    10












    10








    10







    A slight modification of your first grep works for me:



    $ grep -oP '^SuccessfullysbuiltsK[da-z]{12}$' example-output
    9b4624927fa6


    K in PCRE resets the match start:




    The escape sequence K causes any previously matched characters not to
    be included in the final matched sequence.




    It's similar to a zero-width positive look-behind assertion (?<=Successfully...).






    share|improve this answer















    A slight modification of your first grep works for me:



    $ grep -oP '^SuccessfullysbuiltsK[da-z]{12}$' example-output
    9b4624927fa6


    K in PCRE resets the match start:




    The escape sequence K causes any previously matched characters not to
    be included in the final matched sequence.




    It's similar to a zero-width positive look-behind assertion (?<=Successfully...).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 12 at 6:14

























    answered Apr 11 at 8:17









    murumuru

    37.9k590166




    37.9k590166








    • 4





      K is not a look behind assertion, it just sets the start of the matched text here reported by -o. Compare echo aaaaa | grep -Po 'aK.' with echo aaaaa | grep -Po '(?<=a).' (this time using a look-behind assertion).

      – Stéphane Chazelas
      Apr 11 at 16:17






    • 1





      @StéphaneChazelas: To be fair, perlre describes it in the same list item as positive look-behind assertions, with language and typography that strongly suggests thinking of one as a variant of the other (even though that can't be the case at a technical level).

      – Henning Makholm
      Apr 11 at 22:48






    • 1





      @HenningMakholm, yes, it's unfortunate. The pcrepattern man page does not make that confusion. It's true K can often be used in place or positive look behind assertions.

      – Stéphane Chazelas
      Apr 11 at 22:58











    • @StéphaneChazelas thanks, corrected.

      – muru
      Apr 12 at 6:14














    • 4





      K is not a look behind assertion, it just sets the start of the matched text here reported by -o. Compare echo aaaaa | grep -Po 'aK.' with echo aaaaa | grep -Po '(?<=a).' (this time using a look-behind assertion).

      – Stéphane Chazelas
      Apr 11 at 16:17






    • 1





      @StéphaneChazelas: To be fair, perlre describes it in the same list item as positive look-behind assertions, with language and typography that strongly suggests thinking of one as a variant of the other (even though that can't be the case at a technical level).

      – Henning Makholm
      Apr 11 at 22:48






    • 1





      @HenningMakholm, yes, it's unfortunate. The pcrepattern man page does not make that confusion. It's true K can often be used in place or positive look behind assertions.

      – Stéphane Chazelas
      Apr 11 at 22:58











    • @StéphaneChazelas thanks, corrected.

      – muru
      Apr 12 at 6:14








    4




    4





    K is not a look behind assertion, it just sets the start of the matched text here reported by -o. Compare echo aaaaa | grep -Po 'aK.' with echo aaaaa | grep -Po '(?<=a).' (this time using a look-behind assertion).

    – Stéphane Chazelas
    Apr 11 at 16:17





    K is not a look behind assertion, it just sets the start of the matched text here reported by -o. Compare echo aaaaa | grep -Po 'aK.' with echo aaaaa | grep -Po '(?<=a).' (this time using a look-behind assertion).

    – Stéphane Chazelas
    Apr 11 at 16:17




    1




    1





    @StéphaneChazelas: To be fair, perlre describes it in the same list item as positive look-behind assertions, with language and typography that strongly suggests thinking of one as a variant of the other (even though that can't be the case at a technical level).

    – Henning Makholm
    Apr 11 at 22:48





    @StéphaneChazelas: To be fair, perlre describes it in the same list item as positive look-behind assertions, with language and typography that strongly suggests thinking of one as a variant of the other (even though that can't be the case at a technical level).

    – Henning Makholm
    Apr 11 at 22:48




    1




    1





    @HenningMakholm, yes, it's unfortunate. The pcrepattern man page does not make that confusion. It's true K can often be used in place or positive look behind assertions.

    – Stéphane Chazelas
    Apr 11 at 22:58





    @HenningMakholm, yes, it's unfortunate. The pcrepattern man page does not make that confusion. It's true K can often be used in place or positive look behind assertions.

    – Stéphane Chazelas
    Apr 11 at 22:58













    @StéphaneChazelas thanks, corrected.

    – muru
    Apr 12 at 6:14





    @StéphaneChazelas thanks, corrected.

    – muru
    Apr 12 at 6:14













    7














    To get the hexadecimal number at the end of a line that starts with Successfully built, I would probably use sed:



    sed -n -E 's/^Successfully built ([[:xdigit:]]+)$/1/p'


    This replaces the matching line with the hash and prints it (and no other line).



    Or awk:



    awk '/^Successfully built [[:xdigit:]]+$/ { print $NF }'


    This prints the last whitespace-delimited field of each line matching the regular expression.



    In both regular expressions, the [[:xdigit:]]+ expression will match a non-empty string of hexadecimal digits.






    share|improve this answer




























      7














      To get the hexadecimal number at the end of a line that starts with Successfully built, I would probably use sed:



      sed -n -E 's/^Successfully built ([[:xdigit:]]+)$/1/p'


      This replaces the matching line with the hash and prints it (and no other line).



      Or awk:



      awk '/^Successfully built [[:xdigit:]]+$/ { print $NF }'


      This prints the last whitespace-delimited field of each line matching the regular expression.



      In both regular expressions, the [[:xdigit:]]+ expression will match a non-empty string of hexadecimal digits.






      share|improve this answer


























        7












        7








        7







        To get the hexadecimal number at the end of a line that starts with Successfully built, I would probably use sed:



        sed -n -E 's/^Successfully built ([[:xdigit:]]+)$/1/p'


        This replaces the matching line with the hash and prints it (and no other line).



        Or awk:



        awk '/^Successfully built [[:xdigit:]]+$/ { print $NF }'


        This prints the last whitespace-delimited field of each line matching the regular expression.



        In both regular expressions, the [[:xdigit:]]+ expression will match a non-empty string of hexadecimal digits.






        share|improve this answer













        To get the hexadecimal number at the end of a line that starts with Successfully built, I would probably use sed:



        sed -n -E 's/^Successfully built ([[:xdigit:]]+)$/1/p'


        This replaces the matching line with the hash and prints it (and no other line).



        Or awk:



        awk '/^Successfully built [[:xdigit:]]+$/ { print $NF }'


        This prints the last whitespace-delimited field of each line matching the regular expression.



        In both regular expressions, the [[:xdigit:]]+ expression will match a non-empty string of hexadecimal digits.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 11 at 8:11









        KusalanandaKusalananda

        142k18266441




        142k18266441






















            stetoc is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            stetoc is a new contributor. Be nice, and check out our Code of Conduct.













            stetoc is a new contributor. Be nice, and check out our Code of Conduct.












            stetoc is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f511837%2fmerge-two-greps-into-single-one%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

            Plaza Victoria

            In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

            How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...