Compare two files and print the common fields and their corresponding line numbers in the 1st file

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











up vote
2
down vote

favorite












I have some raw data in one file say File1.txt



Colt McCoy QB CLE 135 222 1576 6 9 60.8% 74.51 
Josh Freeman QB TB 291 474 3451 25 6 61.4% 95.9
Matt Cassel QB KC 262 450 3116 27 7 58.2% 93.0
Michael Vick QB PHI 233 372 3018 21 6 62.6% 100.2
Matt Schaub QB HOU 365 574 4370 24 12 63.6% 92.0


File 2.txt has a list of names, one name per line



Josh 
Matt


I want to get the output as Name and line number. For the above example it would be:



Josh: 2 
Matt: 3,5


I have a command which gives me the list of names and the line numbers but I want to print it in the desired format above. My current command is



awk 'print $1, NR' file1.txt | grep -f file2.txt


It shows



Josh 2
Matt 3
Matt 5


What can I do to make it print like the desired format?










share|improve this question



























    up vote
    2
    down vote

    favorite












    I have some raw data in one file say File1.txt



    Colt McCoy QB CLE 135 222 1576 6 9 60.8% 74.51 
    Josh Freeman QB TB 291 474 3451 25 6 61.4% 95.9
    Matt Cassel QB KC 262 450 3116 27 7 58.2% 93.0
    Michael Vick QB PHI 233 372 3018 21 6 62.6% 100.2
    Matt Schaub QB HOU 365 574 4370 24 12 63.6% 92.0


    File 2.txt has a list of names, one name per line



    Josh 
    Matt


    I want to get the output as Name and line number. For the above example it would be:



    Josh: 2 
    Matt: 3,5


    I have a command which gives me the list of names and the line numbers but I want to print it in the desired format above. My current command is



    awk 'print $1, NR' file1.txt | grep -f file2.txt


    It shows



    Josh 2
    Matt 3
    Matt 5


    What can I do to make it print like the desired format?










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have some raw data in one file say File1.txt



      Colt McCoy QB CLE 135 222 1576 6 9 60.8% 74.51 
      Josh Freeman QB TB 291 474 3451 25 6 61.4% 95.9
      Matt Cassel QB KC 262 450 3116 27 7 58.2% 93.0
      Michael Vick QB PHI 233 372 3018 21 6 62.6% 100.2
      Matt Schaub QB HOU 365 574 4370 24 12 63.6% 92.0


      File 2.txt has a list of names, one name per line



      Josh 
      Matt


      I want to get the output as Name and line number. For the above example it would be:



      Josh: 2 
      Matt: 3,5


      I have a command which gives me the list of names and the line numbers but I want to print it in the desired format above. My current command is



      awk 'print $1, NR' file1.txt | grep -f file2.txt


      It shows



      Josh 2
      Matt 3
      Matt 5


      What can I do to make it print like the desired format?










      share|improve this question















      I have some raw data in one file say File1.txt



      Colt McCoy QB CLE 135 222 1576 6 9 60.8% 74.51 
      Josh Freeman QB TB 291 474 3451 25 6 61.4% 95.9
      Matt Cassel QB KC 262 450 3116 27 7 58.2% 93.0
      Michael Vick QB PHI 233 372 3018 21 6 62.6% 100.2
      Matt Schaub QB HOU 365 574 4370 24 12 63.6% 92.0


      File 2.txt has a list of names, one name per line



      Josh 
      Matt


      I want to get the output as Name and line number. For the above example it would be:



      Josh: 2 
      Matt: 3,5


      I have a command which gives me the list of names and the line numbers but I want to print it in the desired format above. My current command is



      awk 'print $1, NR' file1.txt | grep -f file2.txt


      It shows



      Josh 2
      Matt 3
      Matt 5


      What can I do to make it print like the desired format?







      text-processing awk grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 13 '17 at 18:50









      don_crissti

      47.3k15125155




      47.3k15125155










      asked Mar 13 '17 at 18:16









      Raj Kiran

      134




      134




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          awk alone can do this:



          awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
          if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1





          share|improve this answer





























            up vote
            0
            down vote













            You can use diff -y file1 file2.



            It will give you a side-by-side output from both files.
            It also has other options to parse your output.. Review the man page for more options.






            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%2f351207%2fcompare-two-files-and-print-the-common-fields-and-their-corresponding-line-numbe%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              awk alone can do this:



              awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
              if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1





              share|improve this answer


























                up vote
                1
                down vote



                accepted










                awk alone can do this:



                awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
                if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1





                share|improve this answer
























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  awk alone can do this:



                  awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
                  if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1





                  share|improve this answer














                  awk alone can do this:



                  awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
                  if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  answered Mar 13 '17 at 18:47


























                  community wiki





                  don_crissti























                      up vote
                      0
                      down vote













                      You can use diff -y file1 file2.



                      It will give you a side-by-side output from both files.
                      It also has other options to parse your output.. Review the man page for more options.






                      share|improve this answer


























                        up vote
                        0
                        down vote













                        You can use diff -y file1 file2.



                        It will give you a side-by-side output from both files.
                        It also has other options to parse your output.. Review the man page for more options.






                        share|improve this answer
























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You can use diff -y file1 file2.



                          It will give you a side-by-side output from both files.
                          It also has other options to parse your output.. Review the man page for more options.






                          share|improve this answer














                          You can use diff -y file1 file2.



                          It will give you a side-by-side output from both files.
                          It also has other options to parse your output.. Review the man page for more options.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Aug 26 at 4:10

























                          answered Mar 13 '17 at 18:19









                          Moshe Shitrit

                          12




                          12



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f351207%2fcompare-two-files-and-print-the-common-fields-and-their-corresponding-line-numbe%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)