Combine two file into one file

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











up vote
0
down vote

favorite












Suppose I have 2 Files, ABC.txt & DEF.txt with the data shown below as an example:



ABC.txt:



abc 14
dka 1
def 51


DEF.txt:



def 12
ckd 41


I want to grep column 1& 2 from both files and write into a third file so that third file contain both file without any repeated column 1 and thire value put for two file and put zero for values that didn't exit in files. How can it be done?



My expected output is (output:



 ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0






share|improve this question


























    up vote
    0
    down vote

    favorite












    Suppose I have 2 Files, ABC.txt & DEF.txt with the data shown below as an example:



    ABC.txt:



    abc 14
    dka 1
    def 51


    DEF.txt:



    def 12
    ckd 41


    I want to grep column 1& 2 from both files and write into a third file so that third file contain both file without any repeated column 1 and thire value put for two file and put zero for values that didn't exit in files. How can it be done?



    My expected output is (output:



     ABC DEF
    abc 14 0
    ckd 0 41
    def 51 12
    dka 1 0






    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Suppose I have 2 Files, ABC.txt & DEF.txt with the data shown below as an example:



      ABC.txt:



      abc 14
      dka 1
      def 51


      DEF.txt:



      def 12
      ckd 41


      I want to grep column 1& 2 from both files and write into a third file so that third file contain both file without any repeated column 1 and thire value put for two file and put zero for values that didn't exit in files. How can it be done?



      My expected output is (output:



       ABC DEF
      abc 14 0
      ckd 0 41
      def 51 12
      dka 1 0






      share|improve this question














      Suppose I have 2 Files, ABC.txt & DEF.txt with the data shown below as an example:



      ABC.txt:



      abc 14
      dka 1
      def 51


      DEF.txt:



      def 12
      ckd 41


      I want to grep column 1& 2 from both files and write into a third file so that third file contain both file without any repeated column 1 and thire value put for two file and put zero for values that didn't exit in files. How can it be done?



      My expected output is (output:



       ABC DEF
      abc 14 0
      ckd 0 41
      def 51 12
      dka 1 0








      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 15 '17 at 13:48









      0xC0000022L

      7,1051359106




      7,1051359106










      asked Dec 15 '17 at 13:41









      fvosta

      11




      11




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote













          $ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
          abc 14 0
          ckd 0 41
          def 51 12
          dka 1 0


          The header and column spacing are left as an exercise.






          share|improve this answer



























            up vote
            3
            down vote













            Awk solution:



            awk 'BEGIN 
            OFS="t"; print "", "ABC", "DEF"

            NR==FNR a[$1]=$2; next

            if ($1 in a) v=a[$1]; delete a[$1]
            $3 = v+0
            1;
            END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt


            The output:



             ABC DEF
            abc 14 0
            dka 1 0
            def 51 12
            ckd 0 41





            share|improve this answer



























              up vote
              2
              down vote













              GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:



              awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt | 
              datamash -Ws --filler='0' crosstab 1,3 unique 2
              ABC DEF
              abc 14 0
              ckd 0 41
              def 51 12
              dka 1 0





              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%2f411061%2fcombine-two-file-into-one-file%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
                4
                down vote













                $ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
                abc 14 0
                ckd 0 41
                def 51 12
                dka 1 0


                The header and column spacing are left as an exercise.






                share|improve this answer
























                  up vote
                  4
                  down vote













                  $ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
                  abc 14 0
                  ckd 0 41
                  def 51 12
                  dka 1 0


                  The header and column spacing are left as an exercise.






                  share|improve this answer






















                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    $ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
                    abc 14 0
                    ckd 0 41
                    def 51 12
                    dka 1 0


                    The header and column spacing are left as an exercise.






                    share|improve this answer












                    $ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
                    abc 14 0
                    ckd 0 41
                    def 51 12
                    dka 1 0


                    The header and column spacing are left as an exercise.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 15 '17 at 14:25









                    glenn jackman

                    46.7k265103




                    46.7k265103






















                        up vote
                        3
                        down vote













                        Awk solution:



                        awk 'BEGIN 
                        OFS="t"; print "", "ABC", "DEF"

                        NR==FNR a[$1]=$2; next

                        if ($1 in a) v=a[$1]; delete a[$1]
                        $3 = v+0
                        1;
                        END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt


                        The output:



                         ABC DEF
                        abc 14 0
                        dka 1 0
                        def 51 12
                        ckd 0 41





                        share|improve this answer
























                          up vote
                          3
                          down vote













                          Awk solution:



                          awk 'BEGIN 
                          OFS="t"; print "", "ABC", "DEF"

                          NR==FNR a[$1]=$2; next

                          if ($1 in a) v=a[$1]; delete a[$1]
                          $3 = v+0
                          1;
                          END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt


                          The output:



                           ABC DEF
                          abc 14 0
                          dka 1 0
                          def 51 12
                          ckd 0 41





                          share|improve this answer






















                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            Awk solution:



                            awk 'BEGIN 
                            OFS="t"; print "", "ABC", "DEF"

                            NR==FNR a[$1]=$2; next

                            if ($1 in a) v=a[$1]; delete a[$1]
                            $3 = v+0
                            1;
                            END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt


                            The output:



                             ABC DEF
                            abc 14 0
                            dka 1 0
                            def 51 12
                            ckd 0 41





                            share|improve this answer












                            Awk solution:



                            awk 'BEGIN 
                            OFS="t"; print "", "ABC", "DEF"

                            NR==FNR a[$1]=$2; next

                            if ($1 in a) v=a[$1]; delete a[$1]
                            $3 = v+0
                            1;
                            END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt


                            The output:



                             ABC DEF
                            abc 14 0
                            dka 1 0
                            def 51 12
                            ckd 0 41






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 15 '17 at 14:07









                            RomanPerekhrest

                            22.4k12145




                            22.4k12145




















                                up vote
                                2
                                down vote













                                GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:



                                awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt | 
                                datamash -Ws --filler='0' crosstab 1,3 unique 2
                                ABC DEF
                                abc 14 0
                                ckd 0 41
                                def 51 12
                                dka 1 0





                                share|improve this answer
























                                  up vote
                                  2
                                  down vote













                                  GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:



                                  awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt | 
                                  datamash -Ws --filler='0' crosstab 1,3 unique 2
                                  ABC DEF
                                  abc 14 0
                                  ckd 0 41
                                  def 51 12
                                  dka 1 0





                                  share|improve this answer






















                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:



                                    awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt | 
                                    datamash -Ws --filler='0' crosstab 1,3 unique 2
                                    ABC DEF
                                    abc 14 0
                                    ckd 0 41
                                    def 51 12
                                    dka 1 0





                                    share|improve this answer












                                    GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:



                                    awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt | 
                                    datamash -Ws --filler='0' crosstab 1,3 unique 2
                                    ABC DEF
                                    abc 14 0
                                    ckd 0 41
                                    def 51 12
                                    dka 1 0






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 15 '17 at 15:16









                                    steeldriver

                                    31.7k34979




                                    31.7k34979






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411061%2fcombine-two-file-into-one-file%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?