Merge two greps into single one The 2019 Stack Overflow Developer Survey Results Are In Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara 2019 Community Moderator Election ResultsNeed to parse a double entry table with two pairsMerge two lists while removing duplicatesRetrieving two matches from repeated patternGroup matching with grep includes extra charactersGrep on single lineGrep / awk on multiple files to single outputCombine two greps into a single commandone-liner command to rename a file using the output of another commandGrep multiple files and output to multiple files in a single commandGrep 3 Capital Letters and Digits into Two Variables

Why are PDP-7-style microprogrammed instructions out of vogue?

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?

What information about me do stores get via my credit card?

Working through the single responsibility principle (SRP) in Python when calls are expensive

Why can't devices on different VLANs, but on the same subnet, communicate?

"... to apply for a visa" or "... and applied for a visa"?

Why doesn't a hydraulic lever violate conservation of energy?

Is every episode of "Where are my Pants?" identical?

Deal with toxic manager when you can't quit

Can each chord in a progression create its own key?

Are spiders unable to hurt humans, especially very small spiders?

What's the point in a preamp?

Is there a writing software that you can sort scenes like slides in PowerPoint?

How did the crowd guess the pentatonic scale in Bobby McFerrin's presentation?

Was credit for the black hole image misappropriated?

What can I do to 'burn' a journal?

What happens to a Warlock's expended Spell Slots when they gain a Level?

One-dimensional Japanese puzzle

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Is 'stolen' appropriate word?

My body leaves; my core can stay

Is this wall load bearing? Blueprints and photos attached

Store Dynamic-accessible hidden metadata in a cell

Can I visit the Trinity College (Cambridge) library and see some of their rare books



Merge two greps into single one



The 2019 Stack Overflow Developer Survey Results Are In
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
2019 Community Moderator Election ResultsNeed to parse a double entry table with two pairsMerge two lists while removing duplicatesRetrieving two matches from repeated patternGroup matching with grep includes extra charactersGrep on single lineGrep / awk on multiple files to single outputCombine two greps into a single commandone-liner command to rename a file using the output of another commandGrep multiple files and output to multiple files in a single commandGrep 3 Capital Letters and Digits into Two Variables



.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
    yesterday






  • 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
    yesterday











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

    – Kusalananda
    yesterday











  • @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
    yesterday

















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
    yesterday






  • 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
    yesterday











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

    – Kusalananda
    yesterday











  • @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
    yesterday













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 22 hours ago









John Kugelman

1,45111020




1,45111020






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 yesterday









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
    yesterday






  • 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
    yesterday











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

    – Kusalananda
    yesterday











  • @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
    yesterday

















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

    – Sjoerd
    yesterday






  • 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
    yesterday











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

    – Kusalananda
    yesterday











  • @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
    yesterday
















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

– Sjoerd
yesterday





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

– Sjoerd
yesterday




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
yesterday





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
yesterday













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

– Kusalananda
yesterday





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

– Kusalananda
yesterday













@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
yesterday





@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
yesterday










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
    yesterday






  • 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
    yesterday






  • 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
    yesterday











  • @StéphaneChazelas thanks, corrected.

    – muru
    17 hours ago


















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
      yesterday






    • 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
      yesterday






    • 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
      yesterday











    • @StéphaneChazelas thanks, corrected.

      – muru
      17 hours ago















    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
      yesterday






    • 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
      yesterday






    • 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
      yesterday











    • @StéphaneChazelas thanks, corrected.

      – muru
      17 hours ago













    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 17 hours ago

























    answered yesterday









    murumuru

    37.6k589165




    37.6k589165







    • 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
      yesterday






    • 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
      yesterday






    • 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
      yesterday











    • @StéphaneChazelas thanks, corrected.

      – muru
      17 hours ago












    • 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
      yesterday






    • 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
      yesterday






    • 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
      yesterday











    • @StéphaneChazelas thanks, corrected.

      – muru
      17 hours ago







    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
    yesterday





    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
    yesterday




    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
    yesterday





    @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
    yesterday




    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
    yesterday





    @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
    yesterday













    @StéphaneChazelas thanks, corrected.

    – muru
    17 hours ago





    @StéphaneChazelas thanks, corrected.

    – muru
    17 hours ago













    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 yesterday









        KusalanandaKusalananda

        141k17263439




        141k17263439




















            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







            -grep, regular-expression

            Popular posts from this blog

            Creating 100m^2 grid automatically using QGIS?Creating grid constrained within polygon in QGIS?Createing polygon layer from point data using QGIS?Creating vector grid using QGIS?Creating grid polygons from coordinates using R or PythonCreating grid from spatio temporal point data?Creating fields in attributes table using other layers using QGISCreate .shp vector grid in QGISQGIS Creating 4km point grid within polygonsCreate a vector grid over a raster layerVector Grid Creates just one grid

            Why is this plane circling around the Lucknow airport every day?Why do aircraft on Flight Radar 24 jump around randomly sometimes?What airport has this walkway over a taxiway?How does Chicago O'Hare's tower sequence aircraft at peak capacity?Which airport is featured in this Delta commercial?After a crash, for how long is the airport closed?Can a passenger plane stand still in the air, or hover at a fixed location above a ground?What are those trucks towing around, and why?What is this airport outside of Cairo, Egypt?Which US airport has the lowest circling MDH?What is this airport video?

            Nikolai Prilezhaev Bibliography References External links Navigation menuEarly Russian Organic Chemists and Their Legacy092774english translationRussian Biography