How to use grep to get the matching part only, without introducing extra newlines

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a output from a command that I want to pipe to some kind of grep command to obtain the part in each line that matches a given regex, and only output the matching part, without introducing newlines



I know grep -o can do something quite similar but it generates a newline for each match, and the output of my command also contains many new lines.



Example:



input(output of my command):



banana apple banana
apple bananas banned


regex:



ban[a-z]*


output by grep -o:



banana
banana
bananas
banned


expected output:



bananabanana
bananasbanned


One thing I can come up with is to use tr to replace newline character to some character that is unlikely to appear in the output of the command, feed the output to grep -o, and then use tr to get back the newline.



mycommand|tr 'n' @|grep -o regex|tr -d 'n'|tr @ 'n'


However I suppose there should be some better solutions. Any help would be appreciated.







share|improve this question






















  • Are you open to solutions using other tools - such as perl?
    – steeldriver
    Dec 7 '17 at 2:51










  • @steeldriver Yes, I am open to options using other tools though personally I prefer GNU tools. I am not sure about the efficiency but can I assume that perl performs as good as GNU grep, sed, etc.?
    – Weijun Zhou
    Dec 7 '17 at 3:19














up vote
0
down vote

favorite












I have a output from a command that I want to pipe to some kind of grep command to obtain the part in each line that matches a given regex, and only output the matching part, without introducing newlines



I know grep -o can do something quite similar but it generates a newline for each match, and the output of my command also contains many new lines.



Example:



input(output of my command):



banana apple banana
apple bananas banned


regex:



ban[a-z]*


output by grep -o:



banana
banana
bananas
banned


expected output:



bananabanana
bananasbanned


One thing I can come up with is to use tr to replace newline character to some character that is unlikely to appear in the output of the command, feed the output to grep -o, and then use tr to get back the newline.



mycommand|tr 'n' @|grep -o regex|tr -d 'n'|tr @ 'n'


However I suppose there should be some better solutions. Any help would be appreciated.







share|improve this question






















  • Are you open to solutions using other tools - such as perl?
    – steeldriver
    Dec 7 '17 at 2:51










  • @steeldriver Yes, I am open to options using other tools though personally I prefer GNU tools. I am not sure about the efficiency but can I assume that perl performs as good as GNU grep, sed, etc.?
    – Weijun Zhou
    Dec 7 '17 at 3:19












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a output from a command that I want to pipe to some kind of grep command to obtain the part in each line that matches a given regex, and only output the matching part, without introducing newlines



I know grep -o can do something quite similar but it generates a newline for each match, and the output of my command also contains many new lines.



Example:



input(output of my command):



banana apple banana
apple bananas banned


regex:



ban[a-z]*


output by grep -o:



banana
banana
bananas
banned


expected output:



bananabanana
bananasbanned


One thing I can come up with is to use tr to replace newline character to some character that is unlikely to appear in the output of the command, feed the output to grep -o, and then use tr to get back the newline.



mycommand|tr 'n' @|grep -o regex|tr -d 'n'|tr @ 'n'


However I suppose there should be some better solutions. Any help would be appreciated.







share|improve this question














I have a output from a command that I want to pipe to some kind of grep command to obtain the part in each line that matches a given regex, and only output the matching part, without introducing newlines



I know grep -o can do something quite similar but it generates a newline for each match, and the output of my command also contains many new lines.



Example:



input(output of my command):



banana apple banana
apple bananas banned


regex:



ban[a-z]*


output by grep -o:



banana
banana
bananas
banned


expected output:



bananabanana
bananasbanned


One thing I can come up with is to use tr to replace newline character to some character that is unlikely to appear in the output of the command, feed the output to grep -o, and then use tr to get back the newline.



mycommand|tr 'n' @|grep -o regex|tr -d 'n'|tr @ 'n'


However I suppose there should be some better solutions. Any help would be appreciated.









share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '17 at 2:29

























asked Dec 7 '17 at 2:24









Weijun Zhou

1,434119




1,434119











  • Are you open to solutions using other tools - such as perl?
    – steeldriver
    Dec 7 '17 at 2:51










  • @steeldriver Yes, I am open to options using other tools though personally I prefer GNU tools. I am not sure about the efficiency but can I assume that perl performs as good as GNU grep, sed, etc.?
    – Weijun Zhou
    Dec 7 '17 at 3:19
















  • Are you open to solutions using other tools - such as perl?
    – steeldriver
    Dec 7 '17 at 2:51










  • @steeldriver Yes, I am open to options using other tools though personally I prefer GNU tools. I am not sure about the efficiency but can I assume that perl performs as good as GNU grep, sed, etc.?
    – Weijun Zhou
    Dec 7 '17 at 3:19















Are you open to solutions using other tools - such as perl?
– steeldriver
Dec 7 '17 at 2:51




Are you open to solutions using other tools - such as perl?
– steeldriver
Dec 7 '17 at 2:51












@steeldriver Yes, I am open to options using other tools though personally I prefer GNU tools. I am not sure about the efficiency but can I assume that perl performs as good as GNU grep, sed, etc.?
– Weijun Zhou
Dec 7 '17 at 3:19




@steeldriver Yes, I am open to options using other tools though personally I prefer GNU tools. I am not sure about the efficiency but can I assume that perl performs as good as GNU grep, sed, etc.?
– Weijun Zhou
Dec 7 '17 at 3:19










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










If you don't mind using perl:



mycommand | perl -lne 'print join "", /ban[a-z]*/g'


The matches on each line are returned in a list context, which you can join with an empty delimiter.






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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409355%2fhow-to-use-grep-to-get-the-matching-part-only-without-introducing-extra-newline%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    If you don't mind using perl:



    mycommand | perl -lne 'print join "", /ban[a-z]*/g'


    The matches on each line are returned in a list context, which you can join with an empty delimiter.






    share|improve this answer
























      up vote
      2
      down vote



      accepted










      If you don't mind using perl:



      mycommand | perl -lne 'print join "", /ban[a-z]*/g'


      The matches on each line are returned in a list context, which you can join with an empty delimiter.






      share|improve this answer






















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        If you don't mind using perl:



        mycommand | perl -lne 'print join "", /ban[a-z]*/g'


        The matches on each line are returned in a list context, which you can join with an empty delimiter.






        share|improve this answer












        If you don't mind using perl:



        mycommand | perl -lne 'print join "", /ban[a-z]*/g'


        The matches on each line are returned in a list context, which you can join with an empty delimiter.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 7 '17 at 3:39









        steeldriver

        31.8k34979




        31.8k34979



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409355%2fhow-to-use-grep-to-get-the-matching-part-only-without-introducing-extra-newline%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?