How can I print $1 and all matching words on line but no other words?

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











up vote
0
down vote

favorite












The input looks like this:



user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4 groupx groupy
user3 : MPT_group2 MPT_group3 groupn


I want to get back:



user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3


Thanks.







share|improve this question


















  • 1




    What defines "matching" here?
    – nohillside
    Mar 23 at 18:45










  • any word with MPT_* in it.
    – doneladio
    Mar 23 at 22:29














up vote
0
down vote

favorite












The input looks like this:



user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4 groupx groupy
user3 : MPT_group2 MPT_group3 groupn


I want to get back:



user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3


Thanks.







share|improve this question


















  • 1




    What defines "matching" here?
    – nohillside
    Mar 23 at 18:45










  • any word with MPT_* in it.
    – doneladio
    Mar 23 at 22:29












up vote
0
down vote

favorite









up vote
0
down vote

favorite











The input looks like this:



user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4 groupx groupy
user3 : MPT_group2 MPT_group3 groupn


I want to get back:



user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3


Thanks.







share|improve this question














The input looks like this:



user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4 groupx groupy
user3 : MPT_group2 MPT_group3 groupn


I want to get back:



user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3


Thanks.









share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 18:37









Thomas

3,39741123




3,39741123










asked Mar 23 at 18:33









doneladio

31




31







  • 1




    What defines "matching" here?
    – nohillside
    Mar 23 at 18:45










  • any word with MPT_* in it.
    – doneladio
    Mar 23 at 22:29












  • 1




    What defines "matching" here?
    – nohillside
    Mar 23 at 18:45










  • any word with MPT_* in it.
    – doneladio
    Mar 23 at 22:29







1




1




What defines "matching" here?
– nohillside
Mar 23 at 18:45




What defines "matching" here?
– nohillside
Mar 23 at 18:45












any word with MPT_* in it.
– doneladio
Mar 23 at 22:29




any word with MPT_* in it.
– doneladio
Mar 23 at 22:29










3 Answers
3






active

oldest

votes

















up vote
0
down vote













An awk solution:



$ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3


To make that more readable, here is the bare awk script portion:




printf "%s : ", $1;
for( i=3; i<=NF; i++ )
if( $i ~ /^MPT_group/ )
printf "%s ", $i

print ""






share|improve this answer



























    up vote
    0
    down vote













    Awk solution:



    awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file


    The output:



    user1 : MPT_group1
    user2 : MPT_group1 MPT_group3 MPT_group4
    user3 : MPT_group2 MPT_group3





    share|improve this answer




















    • Perfect. Thanks!
      – doneladio
      Mar 23 at 22:17

















    up vote
    0
    down vote













    With grep:



    $ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
    user1 : MPT_group1
    user2 : MPT_group1 MPT_group3 MPT_group4
    user3 : MPT_group2 MPT_group3





    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%2f433119%2fhow-can-i-print-1-and-all-matching-words-on-line-but-no-other-words%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      An awk solution:



      $ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
      user1 : MPT_group1
      user2 : MPT_group1 MPT_group3 MPT_group4
      user3 : MPT_group2 MPT_group3


      To make that more readable, here is the bare awk script portion:




      printf "%s : ", $1;
      for( i=3; i<=NF; i++ )
      if( $i ~ /^MPT_group/ )
      printf "%s ", $i

      print ""






      share|improve this answer
























        up vote
        0
        down vote













        An awk solution:



        $ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
        user1 : MPT_group1
        user2 : MPT_group1 MPT_group3 MPT_group4
        user3 : MPT_group2 MPT_group3


        To make that more readable, here is the bare awk script portion:




        printf "%s : ", $1;
        for( i=3; i<=NF; i++ )
        if( $i ~ /^MPT_group/ )
        printf "%s ", $i

        print ""






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          An awk solution:



          $ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
          user1 : MPT_group1
          user2 : MPT_group1 MPT_group3 MPT_group4
          user3 : MPT_group2 MPT_group3


          To make that more readable, here is the bare awk script portion:




          printf "%s : ", $1;
          for( i=3; i<=NF; i++ )
          if( $i ~ /^MPT_group/ )
          printf "%s ", $i

          print ""






          share|improve this answer












          An awk solution:



          $ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
          user1 : MPT_group1
          user2 : MPT_group1 MPT_group3 MPT_group4
          user3 : MPT_group2 MPT_group3


          To make that more readable, here is the bare awk script portion:




          printf "%s : ", $1;
          for( i=3; i<=NF; i++ )
          if( $i ~ /^MPT_group/ )
          printf "%s ", $i

          print ""







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 at 18:45









          DopeGhoti

          40.2k54779




          40.2k54779






















              up vote
              0
              down vote













              Awk solution:



              awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file


              The output:



              user1 : MPT_group1
              user2 : MPT_group1 MPT_group3 MPT_group4
              user3 : MPT_group2 MPT_group3





              share|improve this answer




















              • Perfect. Thanks!
                – doneladio
                Mar 23 at 22:17














              up vote
              0
              down vote













              Awk solution:



              awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file


              The output:



              user1 : MPT_group1
              user2 : MPT_group1 MPT_group3 MPT_group4
              user3 : MPT_group2 MPT_group3





              share|improve this answer




















              • Perfect. Thanks!
                – doneladio
                Mar 23 at 22:17












              up vote
              0
              down vote










              up vote
              0
              down vote









              Awk solution:



              awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file


              The output:



              user1 : MPT_group1
              user2 : MPT_group1 MPT_group3 MPT_group4
              user3 : MPT_group2 MPT_group3





              share|improve this answer












              Awk solution:



              awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file


              The output:



              user1 : MPT_group1
              user2 : MPT_group1 MPT_group3 MPT_group4
              user3 : MPT_group2 MPT_group3






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 23 at 18:45









              RomanPerekhrest

              22.4k12144




              22.4k12144











              • Perfect. Thanks!
                – doneladio
                Mar 23 at 22:17
















              • Perfect. Thanks!
                – doneladio
                Mar 23 at 22:17















              Perfect. Thanks!
              – doneladio
              Mar 23 at 22:17




              Perfect. Thanks!
              – doneladio
              Mar 23 at 22:17










              up vote
              0
              down vote













              With grep:



              $ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
              user1 : MPT_group1
              user2 : MPT_group1 MPT_group3 MPT_group4
              user3 : MPT_group2 MPT_group3





              share|improve this answer
























                up vote
                0
                down vote













                With grep:



                $ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
                user1 : MPT_group1
                user2 : MPT_group1 MPT_group3 MPT_group4
                user3 : MPT_group2 MPT_group3





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  With grep:



                  $ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
                  user1 : MPT_group1
                  user2 : MPT_group1 MPT_group3 MPT_group4
                  user3 : MPT_group2 MPT_group3





                  share|improve this answer












                  With grep:



                  $ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
                  user1 : MPT_group1
                  user2 : MPT_group1 MPT_group3 MPT_group4
                  user3 : MPT_group2 MPT_group3






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 23 at 18:56









                  αғsнιη

                  14.8k82462




                  14.8k82462






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f433119%2fhow-can-i-print-1-and-all-matching-words-on-line-but-no-other-words%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?