How to merge two files with different lengths and columns linux

Multi tool use
Multi tool use

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











up vote
1
down vote

favorite












I have 2 files with different columns and length and I want to merge them as desired output file given below, Please Help!!!



File 1



aa
bb
cc
dd


File 2



ff , 2 , tg12
dd , 3 , tg13
gg , 4 , tg14
hh , 5 , tg15
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
ii , 9 , tg19


Desired output



aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13






share|improve this question






















  • format your question to make it readable
    – RomanPerekhrest
    Nov 5 '17 at 20:11










  • @steeldriver can you suggest how can I do that?
    – Amber
    Nov 5 '17 at 20:22










  • @Amber, is the order of lines by "starting field" matters?
    – RomanPerekhrest
    Nov 5 '17 at 20:27











  • @RomanPerekhrest yes the order of line matters
    – Amber
    Nov 5 '17 at 20:29














up vote
1
down vote

favorite












I have 2 files with different columns and length and I want to merge them as desired output file given below, Please Help!!!



File 1



aa
bb
cc
dd


File 2



ff , 2 , tg12
dd , 3 , tg13
gg , 4 , tg14
hh , 5 , tg15
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
ii , 9 , tg19


Desired output



aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13






share|improve this question






















  • format your question to make it readable
    – RomanPerekhrest
    Nov 5 '17 at 20:11










  • @steeldriver can you suggest how can I do that?
    – Amber
    Nov 5 '17 at 20:22










  • @Amber, is the order of lines by "starting field" matters?
    – RomanPerekhrest
    Nov 5 '17 at 20:27











  • @RomanPerekhrest yes the order of line matters
    – Amber
    Nov 5 '17 at 20:29












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have 2 files with different columns and length and I want to merge them as desired output file given below, Please Help!!!



File 1



aa
bb
cc
dd


File 2



ff , 2 , tg12
dd , 3 , tg13
gg , 4 , tg14
hh , 5 , tg15
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
ii , 9 , tg19


Desired output



aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13






share|improve this question














I have 2 files with different columns and length and I want to merge them as desired output file given below, Please Help!!!



File 1



aa
bb
cc
dd


File 2



ff , 2 , tg12
dd , 3 , tg13
gg , 4 , tg14
hh , 5 , tg15
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
ii , 9 , tg19


Desired output



aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13








share|improve this question













share|improve this question




share|improve this question








edited Nov 5 '17 at 20:26









Thor

11.1k13256




11.1k13256










asked Nov 5 '17 at 19:58









Amber

92




92











  • format your question to make it readable
    – RomanPerekhrest
    Nov 5 '17 at 20:11










  • @steeldriver can you suggest how can I do that?
    – Amber
    Nov 5 '17 at 20:22










  • @Amber, is the order of lines by "starting field" matters?
    – RomanPerekhrest
    Nov 5 '17 at 20:27











  • @RomanPerekhrest yes the order of line matters
    – Amber
    Nov 5 '17 at 20:29
















  • format your question to make it readable
    – RomanPerekhrest
    Nov 5 '17 at 20:11










  • @steeldriver can you suggest how can I do that?
    – Amber
    Nov 5 '17 at 20:22










  • @Amber, is the order of lines by "starting field" matters?
    – RomanPerekhrest
    Nov 5 '17 at 20:27











  • @RomanPerekhrest yes the order of line matters
    – Amber
    Nov 5 '17 at 20:29















format your question to make it readable
– RomanPerekhrest
Nov 5 '17 at 20:11




format your question to make it readable
– RomanPerekhrest
Nov 5 '17 at 20:11












@steeldriver can you suggest how can I do that?
– Amber
Nov 5 '17 at 20:22




@steeldriver can you suggest how can I do that?
– Amber
Nov 5 '17 at 20:22












@Amber, is the order of lines by "starting field" matters?
– RomanPerekhrest
Nov 5 '17 at 20:27





@Amber, is the order of lines by "starting field" matters?
– RomanPerekhrest
Nov 5 '17 at 20:27













@RomanPerekhrest yes the order of line matters
– Amber
Nov 5 '17 at 20:29




@RomanPerekhrest yes the order of line matters
– Amber
Nov 5 '17 at 20:29










2 Answers
2






active

oldest

votes

















up vote
1
down vote













AWK solution (ordered by sort command):



awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort



  • FS='[[:space:]]*,[[:space:]]' - field separator that is set for file2


The output:



aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13





share|improve this answer





























    up vote
    0
    down vote













    The simplest solution I could think of:



    grep -f file1 file2 | sort





    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%2f402716%2fhow-to-merge-two-files-with-different-lengths-and-columns-linux%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













      AWK solution (ordered by sort command):



      awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort



      • FS='[[:space:]]*,[[:space:]]' - field separator that is set for file2


      The output:



      aa , 6 , tg16
      bb , 7 , tg17
      cc , 8 , tg18
      dd , 3 , tg13





      share|improve this answer


























        up vote
        1
        down vote













        AWK solution (ordered by sort command):



        awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort



        • FS='[[:space:]]*,[[:space:]]' - field separator that is set for file2


        The output:



        aa , 6 , tg16
        bb , 7 , tg17
        cc , 8 , tg18
        dd , 3 , tg13





        share|improve this answer
























          up vote
          1
          down vote










          up vote
          1
          down vote









          AWK solution (ordered by sort command):



          awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort



          • FS='[[:space:]]*,[[:space:]]' - field separator that is set for file2


          The output:



          aa , 6 , tg16
          bb , 7 , tg17
          cc , 8 , tg18
          dd , 3 , tg13





          share|improve this answer














          AWK solution (ordered by sort command):



          awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort



          • FS='[[:space:]]*,[[:space:]]' - field separator that is set for file2


          The output:



          aa , 6 , tg16
          bb , 7 , tg17
          cc , 8 , tg18
          dd , 3 , tg13






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 5 '17 at 20:35

























          answered Nov 5 '17 at 20:28









          RomanPerekhrest

          22.5k12145




          22.5k12145






















              up vote
              0
              down vote













              The simplest solution I could think of:



              grep -f file1 file2 | sort





              share|improve this answer
























                up vote
                0
                down vote













                The simplest solution I could think of:



                grep -f file1 file2 | sort





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  The simplest solution I could think of:



                  grep -f file1 file2 | sort





                  share|improve this answer












                  The simplest solution I could think of:



                  grep -f file1 file2 | sort






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 6 '17 at 8:34









                  Michael Vehrs

                  2,17037




                  2,17037



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402716%2fhow-to-merge-two-files-with-different-lengths-and-columns-linux%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      V811v,jTgjBcApmxzmSQnE2iHKymo,0LQX APnqK5L2fY4Aq,yf8hXqXTn,r0,LnF6v5tuL5kyZoenG6n2yfSg 81ilB
                      ba0lhAJ23kzCHntV F7c2DtdL1xGJpwfgqk UANr13juYxmKcLF

                      Popular posts from this blog

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

                      How many registers does an x86_64 CPU actually have?

                      Displaying single band from multi-band raster using QGIS