How to merge two files of different lines and column and output matching lines with colums?

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











up vote
3
down vote

favorite












I have file1 (sample):



60108903374
60172485121
60108919381
60174213128
60108919951
60108919970
601112020106
601112020107
601112020108
601112020113
601112020114
60175472940


And file2:



60179970001,A
60172681920,A
60174202041,A
60172514180,A
60174314679,A
60174325306,A
60175472940,A
60174213128,A
60175328984,A
60175349857,A
60172796759,A
60172798922,A
60179195129,A
60172485121,B
60173483126,A
60172683175,A
60174521828,A
60142536314,B
60175347909,B
60175183031,B


I want to merge file1 and file2 with output matching based on the first column as well as shows the second column from file2.



Desired output:



60172485121,B
60174213128,A


file1 has ~80k lines and file2 has 500k lines.



Tried using:



join -1 1 -2 1 -o 1.1,2.2 file1 file2









share|improve this question























  • It is not important bu I think 60175472940,A is missing in the "desired output"
    – JJoao
    Sep 9 '16 at 9:17














up vote
3
down vote

favorite












I have file1 (sample):



60108903374
60172485121
60108919381
60174213128
60108919951
60108919970
601112020106
601112020107
601112020108
601112020113
601112020114
60175472940


And file2:



60179970001,A
60172681920,A
60174202041,A
60172514180,A
60174314679,A
60174325306,A
60175472940,A
60174213128,A
60175328984,A
60175349857,A
60172796759,A
60172798922,A
60179195129,A
60172485121,B
60173483126,A
60172683175,A
60174521828,A
60142536314,B
60175347909,B
60175183031,B


I want to merge file1 and file2 with output matching based on the first column as well as shows the second column from file2.



Desired output:



60172485121,B
60174213128,A


file1 has ~80k lines and file2 has 500k lines.



Tried using:



join -1 1 -2 1 -o 1.1,2.2 file1 file2









share|improve this question























  • It is not important bu I think 60175472940,A is missing in the "desired output"
    – JJoao
    Sep 9 '16 at 9:17












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have file1 (sample):



60108903374
60172485121
60108919381
60174213128
60108919951
60108919970
601112020106
601112020107
601112020108
601112020113
601112020114
60175472940


And file2:



60179970001,A
60172681920,A
60174202041,A
60172514180,A
60174314679,A
60174325306,A
60175472940,A
60174213128,A
60175328984,A
60175349857,A
60172796759,A
60172798922,A
60179195129,A
60172485121,B
60173483126,A
60172683175,A
60174521828,A
60142536314,B
60175347909,B
60175183031,B


I want to merge file1 and file2 with output matching based on the first column as well as shows the second column from file2.



Desired output:



60172485121,B
60174213128,A


file1 has ~80k lines and file2 has 500k lines.



Tried using:



join -1 1 -2 1 -o 1.1,2.2 file1 file2









share|improve this question















I have file1 (sample):



60108903374
60172485121
60108919381
60174213128
60108919951
60108919970
601112020106
601112020107
601112020108
601112020113
601112020114
60175472940


And file2:



60179970001,A
60172681920,A
60174202041,A
60172514180,A
60174314679,A
60174325306,A
60175472940,A
60174213128,A
60175328984,A
60175349857,A
60172796759,A
60172798922,A
60179195129,A
60172485121,B
60173483126,A
60172683175,A
60174521828,A
60142536314,B
60175347909,B
60175183031,B


I want to merge file1 and file2 with output matching based on the first column as well as shows the second column from file2.



Desired output:



60172485121,B
60174213128,A


file1 has ~80k lines and file2 has 500k lines.



Tried using:



join -1 1 -2 1 -o 1.1,2.2 file1 file2






text-processing join






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 0:31









Rui F Ribeiro

38.2k1475123




38.2k1475123










asked Sep 6 '16 at 13:46









Johan Hakim Fahmi

444




444











  • It is not important bu I think 60175472940,A is missing in the "desired output"
    – JJoao
    Sep 9 '16 at 9:17
















  • It is not important bu I think 60175472940,A is missing in the "desired output"
    – JJoao
    Sep 9 '16 at 9:17















It is not important bu I think 60175472940,A is missing in the "desired output"
– JJoao
Sep 9 '16 at 9:17




It is not important bu I think 60175472940,A is missing in the "desired output"
– JJoao
Sep 9 '16 at 9:17










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










join -t, <(sort file1) <(sort -t, file2)


The above does the job.






share|improve this answer





























    up vote
    0
    down vote













    awk -F, 'NF==1 a[$1]++; 
    NF>=2 && a[$1]' file1 file2




    • if we have just one field, save it

    • if we have more and the first field is saved, print it

    output obtained:



    60175472940,A
    60174213128,A
    60172485121,B





    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: 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
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f308201%2fhow-to-merge-two-files-of-different-lines-and-column-and-output-matching-lines-w%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








      up vote
      2
      down vote



      accepted










      join -t, <(sort file1) <(sort -t, file2)


      The above does the job.






      share|improve this answer


























        up vote
        2
        down vote



        accepted










        join -t, <(sort file1) <(sort -t, file2)


        The above does the job.






        share|improve this answer
























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          join -t, <(sort file1) <(sort -t, file2)


          The above does the job.






          share|improve this answer














          join -t, <(sort file1) <(sort -t, file2)


          The above does the job.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 6 '16 at 15:18









          Scott

          6,57142650




          6,57142650










          answered Sep 6 '16 at 14:50









          Johan Hakim Fahmi

          444




          444






















              up vote
              0
              down vote













              awk -F, 'NF==1 a[$1]++; 
              NF>=2 && a[$1]' file1 file2




              • if we have just one field, save it

              • if we have more and the first field is saved, print it

              output obtained:



              60175472940,A
              60174213128,A
              60172485121,B





              share|improve this answer
























                up vote
                0
                down vote













                awk -F, 'NF==1 a[$1]++; 
                NF>=2 && a[$1]' file1 file2




                • if we have just one field, save it

                • if we have more and the first field is saved, print it

                output obtained:



                60175472940,A
                60174213128,A
                60172485121,B





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  awk -F, 'NF==1 a[$1]++; 
                  NF>=2 && a[$1]' file1 file2




                  • if we have just one field, save it

                  • if we have more and the first field is saved, print it

                  output obtained:



                  60175472940,A
                  60174213128,A
                  60172485121,B





                  share|improve this answer












                  awk -F, 'NF==1 a[$1]++; 
                  NF>=2 && a[$1]' file1 file2




                  • if we have just one field, save it

                  • if we have more and the first field is saved, print it

                  output obtained:



                  60175472940,A
                  60174213128,A
                  60172485121,B






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 6 '16 at 15:55









                  JJoao

                  6,9441826




                  6,9441826



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f308201%2fhow-to-merge-two-files-of-different-lines-and-column-and-output-matching-lines-w%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

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

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay