awk: print duplicates from two files

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











up vote
0
down vote

favorite












File1:



23455|abc|xyz
23455|abc|xsd
34433|wer|sad
45655|fdf|fcd


File2:



v343v|23455
z565z|23455
c9898|34433
b2323|45655


Output should be:



23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323


I am using the below command :



awk -F'|' 'NR==FNR a[$1]=$1" $2 > 0 " $1' file1 file2 > result.txt


But its only showing the result :



23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323


Note: Both the files have data in random order







share|improve this question

























    up vote
    0
    down vote

    favorite












    File1:



    23455|abc|xyz
    23455|abc|xsd
    34433|wer|sad
    45655|fdf|fcd


    File2:



    v343v|23455
    z565z|23455
    c9898|34433
    b2323|45655


    Output should be:



    23455|abc|xyz|v343v
    23455|abc|xyz|z565z
    23455|abc|xsd|v343v
    23455|abc|xsd|z565z
    34433|wer|sad|c9898
    45655|fdf|fcd|b2323


    I am using the below command :



    awk -F'|' 'NR==FNR a[$1]=$1" $2 > 0 " $1' file1 file2 > result.txt


    But its only showing the result :



    23455|abc|xsd|v343v
    23455|abc|xsd|z565z
    34433|wer|sad|c9898
    45655|fdf|fcd|b2323


    Note: Both the files have data in random order







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      File1:



      23455|abc|xyz
      23455|abc|xsd
      34433|wer|sad
      45655|fdf|fcd


      File2:



      v343v|23455
      z565z|23455
      c9898|34433
      b2323|45655


      Output should be:



      23455|abc|xyz|v343v
      23455|abc|xyz|z565z
      23455|abc|xsd|v343v
      23455|abc|xsd|z565z
      34433|wer|sad|c9898
      45655|fdf|fcd|b2323


      I am using the below command :



      awk -F'|' 'NR==FNR a[$1]=$1" $2 > 0 " $1' file1 file2 > result.txt


      But its only showing the result :



      23455|abc|xsd|v343v
      23455|abc|xsd|z565z
      34433|wer|sad|c9898
      45655|fdf|fcd|b2323


      Note: Both the files have data in random order







      share|improve this question













      File1:



      23455|abc|xyz
      23455|abc|xsd
      34433|wer|sad
      45655|fdf|fcd


      File2:



      v343v|23455
      z565z|23455
      c9898|34433
      b2323|45655


      Output should be:



      23455|abc|xyz|v343v
      23455|abc|xyz|z565z
      23455|abc|xsd|v343v
      23455|abc|xsd|z565z
      34433|wer|sad|c9898
      45655|fdf|fcd|b2323


      I am using the below command :



      awk -F'|' 'NR==FNR a[$1]=$1" $2 > 0 " $1' file1 file2 > result.txt


      But its only showing the result :



      23455|abc|xsd|v343v
      23455|abc|xsd|z565z
      34433|wer|sad|c9898
      45655|fdf|fcd|b2323


      Note: Both the files have data in random order









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 13 at 10:25
























      asked Jun 13 at 10:01









      Maddys

      33




      33




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          $ join -t '|' -2 2 file1 file2
          23455|abc|xyz|v343v
          23455|abc|xyz|z565z
          23455|abc|xsd|v343v
          23455|abc|xsd|z565z
          34433|wer|sad|c9898
          45655|fdf|fcd|b2323


          This performs a relational JOIN operation on the two files. The -t '|' tells join that | is used as a field delimiter in the input data, and -2 2 tells it to use the second field in the second of the two files (rather than the first).



          This assumes that the join column is sorted in the two files.



          Are they not sorted, then pre-sort the files using



          sort -t '|' -k1 -o file1 file1
          sort -t '|' -k2 -o file2 file2


          or sort at the same time as you call join using a process substitution in a shell that supports this:



          join -t '|' -2 2 
          <( sort -t '|' -k1 file1 )
          <( sort -t '|' -k2 file2 )





          share|improve this answer























          • thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
            – Maddys
            Jun 13 at 10:23











          • @Maddys See updated answer.
            – Kusalananda
            Jun 13 at 10:33

















          up vote
          0
          down vote













          Using awk:



          awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2


          The for loops trough all lines of file1 stored in the array a. If the first element of the second file matches and array entry, print it.






          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%2f449495%2fawk-print-duplicates-from-two-files%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










            $ join -t '|' -2 2 file1 file2
            23455|abc|xyz|v343v
            23455|abc|xyz|z565z
            23455|abc|xsd|v343v
            23455|abc|xsd|z565z
            34433|wer|sad|c9898
            45655|fdf|fcd|b2323


            This performs a relational JOIN operation on the two files. The -t '|' tells join that | is used as a field delimiter in the input data, and -2 2 tells it to use the second field in the second of the two files (rather than the first).



            This assumes that the join column is sorted in the two files.



            Are they not sorted, then pre-sort the files using



            sort -t '|' -k1 -o file1 file1
            sort -t '|' -k2 -o file2 file2


            or sort at the same time as you call join using a process substitution in a shell that supports this:



            join -t '|' -2 2 
            <( sort -t '|' -k1 file1 )
            <( sort -t '|' -k2 file2 )





            share|improve this answer























            • thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
              – Maddys
              Jun 13 at 10:23











            • @Maddys See updated answer.
              – Kusalananda
              Jun 13 at 10:33














            up vote
            1
            down vote



            accepted










            $ join -t '|' -2 2 file1 file2
            23455|abc|xyz|v343v
            23455|abc|xyz|z565z
            23455|abc|xsd|v343v
            23455|abc|xsd|z565z
            34433|wer|sad|c9898
            45655|fdf|fcd|b2323


            This performs a relational JOIN operation on the two files. The -t '|' tells join that | is used as a field delimiter in the input data, and -2 2 tells it to use the second field in the second of the two files (rather than the first).



            This assumes that the join column is sorted in the two files.



            Are they not sorted, then pre-sort the files using



            sort -t '|' -k1 -o file1 file1
            sort -t '|' -k2 -o file2 file2


            or sort at the same time as you call join using a process substitution in a shell that supports this:



            join -t '|' -2 2 
            <( sort -t '|' -k1 file1 )
            <( sort -t '|' -k2 file2 )





            share|improve this answer























            • thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
              – Maddys
              Jun 13 at 10:23











            • @Maddys See updated answer.
              – Kusalananda
              Jun 13 at 10:33












            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            $ join -t '|' -2 2 file1 file2
            23455|abc|xyz|v343v
            23455|abc|xyz|z565z
            23455|abc|xsd|v343v
            23455|abc|xsd|z565z
            34433|wer|sad|c9898
            45655|fdf|fcd|b2323


            This performs a relational JOIN operation on the two files. The -t '|' tells join that | is used as a field delimiter in the input data, and -2 2 tells it to use the second field in the second of the two files (rather than the first).



            This assumes that the join column is sorted in the two files.



            Are they not sorted, then pre-sort the files using



            sort -t '|' -k1 -o file1 file1
            sort -t '|' -k2 -o file2 file2


            or sort at the same time as you call join using a process substitution in a shell that supports this:



            join -t '|' -2 2 
            <( sort -t '|' -k1 file1 )
            <( sort -t '|' -k2 file2 )





            share|improve this answer















            $ join -t '|' -2 2 file1 file2
            23455|abc|xyz|v343v
            23455|abc|xyz|z565z
            23455|abc|xsd|v343v
            23455|abc|xsd|z565z
            34433|wer|sad|c9898
            45655|fdf|fcd|b2323


            This performs a relational JOIN operation on the two files. The -t '|' tells join that | is used as a field delimiter in the input data, and -2 2 tells it to use the second field in the second of the two files (rather than the first).



            This assumes that the join column is sorted in the two files.



            Are they not sorted, then pre-sort the files using



            sort -t '|' -k1 -o file1 file1
            sort -t '|' -k2 -o file2 file2


            or sort at the same time as you call join using a process substitution in a shell that supports this:



            join -t '|' -2 2 
            <( sort -t '|' -k1 file1 )
            <( sort -t '|' -k2 file2 )






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jun 13 at 10:33


























            answered Jun 13 at 10:13









            Kusalananda

            101k13199312




            101k13199312











            • thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
              – Maddys
              Jun 13 at 10:23











            • @Maddys See updated answer.
              – Kusalananda
              Jun 13 at 10:33
















            • thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
              – Maddys
              Jun 13 at 10:23











            • @Maddys See updated answer.
              – Kusalananda
              Jun 13 at 10:33















            thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
            – Maddys
            Jun 13 at 10:23





            thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
            – Maddys
            Jun 13 at 10:23













            @Maddys See updated answer.
            – Kusalananda
            Jun 13 at 10:33




            @Maddys See updated answer.
            – Kusalananda
            Jun 13 at 10:33












            up vote
            0
            down vote













            Using awk:



            awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2


            The for loops trough all lines of file1 stored in the array a. If the first element of the second file matches and array entry, print it.






            share|improve this answer

























              up vote
              0
              down vote













              Using awk:



              awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2


              The for loops trough all lines of file1 stored in the array a. If the first element of the second file matches and array entry, print it.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Using awk:



                awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2


                The for loops trough all lines of file1 stored in the array a. If the first element of the second file matches and array entry, print it.






                share|improve this answer













                Using awk:



                awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2


                The for loops trough all lines of file1 stored in the array a. If the first element of the second file matches and array entry, print it.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jun 13 at 11:10









                oliv

                85927




                85927






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449495%2fawk-print-duplicates-from-two-files%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?