Use Awk to get data from two files











up vote
2
down vote

favorite












I have two different files with two columns each.



file1.txt



DevId   Group
aaa A
bbb B


file2.txt



Group   RefId
A 111-222-333
B 444-555-666


I need only need DevId and its corresponding RefId.



Required Output



DevId   RefId
aaa 111-222-333
bbb 444-555-666


I tried using this syntax but I can't get it correctly.



awk -F, -v OFS=, 'NR==FNR{a[$1]=$2;next}{print a[$2],$1}' file2.txt file1.txt


I hope someone could help me.










share|improve this question




























    up vote
    2
    down vote

    favorite












    I have two different files with two columns each.



    file1.txt



    DevId   Group
    aaa A
    bbb B


    file2.txt



    Group   RefId
    A 111-222-333
    B 444-555-666


    I need only need DevId and its corresponding RefId.



    Required Output



    DevId   RefId
    aaa 111-222-333
    bbb 444-555-666


    I tried using this syntax but I can't get it correctly.



    awk -F, -v OFS=, 'NR==FNR{a[$1]=$2;next}{print a[$2],$1}' file2.txt file1.txt


    I hope someone could help me.










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have two different files with two columns each.



      file1.txt



      DevId   Group
      aaa A
      bbb B


      file2.txt



      Group   RefId
      A 111-222-333
      B 444-555-666


      I need only need DevId and its corresponding RefId.



      Required Output



      DevId   RefId
      aaa 111-222-333
      bbb 444-555-666


      I tried using this syntax but I can't get it correctly.



      awk -F, -v OFS=, 'NR==FNR{a[$1]=$2;next}{print a[$2],$1}' file2.txt file1.txt


      I hope someone could help me.










      share|improve this question















      I have two different files with two columns each.



      file1.txt



      DevId   Group
      aaa A
      bbb B


      file2.txt



      Group   RefId
      A 111-222-333
      B 444-555-666


      I need only need DevId and its corresponding RefId.



      Required Output



      DevId   RefId
      aaa 111-222-333
      bbb 444-555-666


      I tried using this syntax but I can't get it correctly.



      awk -F, -v OFS=, 'NR==FNR{a[$1]=$2;next}{print a[$2],$1}' file2.txt file1.txt


      I hope someone could help me.







      awk lookup






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 6:01









      Inian

      38.3k63669




      38.3k63669










      asked Nov 10 at 5:51









      jecha

      182




      182
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Here:



          awk -v RS="rn" 'FNR==NR{a[$1]=$2;next}{ print $1, a[$2]}' file2.txt file1.txt


          This was modified from Awk multiple files which I suggest you read for the explanation.



          Edit: As mentioned by @JamesBrown, added -v RS="rn" for line endings






          share|improve this answer























          • I tried this. But I can only get DevId. I'll read that. Thanks!
            – jecha
            Nov 10 at 6:51










          • What do you mean you "can only get DevId"? I've tried this in my local machine and it outputs your required output.
            – ssemilla
            Nov 10 at 6:59










          • The output are only values under DevId.
            – jecha
            Nov 10 at 7:11






          • 3




            OP, you have rn line endings. Use awk -v RS="rn" ... The same fixes @RavinderSingh13's answer as well.
            – James Brown
            Nov 10 at 7:15












          • @jecha, I updated the answer thanks to @JamesBrown. I would also suggest using dos2unix when working with files in in Linux if you can since most of unix tools are expecting unix line endings.
            – ssemilla
            Nov 10 at 7:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53236357%2fuse-awk-to-get-data-from-two-files%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Here:



          awk -v RS="rn" 'FNR==NR{a[$1]=$2;next}{ print $1, a[$2]}' file2.txt file1.txt


          This was modified from Awk multiple files which I suggest you read for the explanation.



          Edit: As mentioned by @JamesBrown, added -v RS="rn" for line endings






          share|improve this answer























          • I tried this. But I can only get DevId. I'll read that. Thanks!
            – jecha
            Nov 10 at 6:51










          • What do you mean you "can only get DevId"? I've tried this in my local machine and it outputs your required output.
            – ssemilla
            Nov 10 at 6:59










          • The output are only values under DevId.
            – jecha
            Nov 10 at 7:11






          • 3




            OP, you have rn line endings. Use awk -v RS="rn" ... The same fixes @RavinderSingh13's answer as well.
            – James Brown
            Nov 10 at 7:15












          • @jecha, I updated the answer thanks to @JamesBrown. I would also suggest using dos2unix when working with files in in Linux if you can since most of unix tools are expecting unix line endings.
            – ssemilla
            Nov 10 at 7:21















          up vote
          2
          down vote



          accepted










          Here:



          awk -v RS="rn" 'FNR==NR{a[$1]=$2;next}{ print $1, a[$2]}' file2.txt file1.txt


          This was modified from Awk multiple files which I suggest you read for the explanation.



          Edit: As mentioned by @JamesBrown, added -v RS="rn" for line endings






          share|improve this answer























          • I tried this. But I can only get DevId. I'll read that. Thanks!
            – jecha
            Nov 10 at 6:51










          • What do you mean you "can only get DevId"? I've tried this in my local machine and it outputs your required output.
            – ssemilla
            Nov 10 at 6:59










          • The output are only values under DevId.
            – jecha
            Nov 10 at 7:11






          • 3




            OP, you have rn line endings. Use awk -v RS="rn" ... The same fixes @RavinderSingh13's answer as well.
            – James Brown
            Nov 10 at 7:15












          • @jecha, I updated the answer thanks to @JamesBrown. I would also suggest using dos2unix when working with files in in Linux if you can since most of unix tools are expecting unix line endings.
            – ssemilla
            Nov 10 at 7:21













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Here:



          awk -v RS="rn" 'FNR==NR{a[$1]=$2;next}{ print $1, a[$2]}' file2.txt file1.txt


          This was modified from Awk multiple files which I suggest you read for the explanation.



          Edit: As mentioned by @JamesBrown, added -v RS="rn" for line endings






          share|improve this answer














          Here:



          awk -v RS="rn" 'FNR==NR{a[$1]=$2;next}{ print $1, a[$2]}' file2.txt file1.txt


          This was modified from Awk multiple files which I suggest you read for the explanation.



          Edit: As mentioned by @JamesBrown, added -v RS="rn" for line endings







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 8:32

























          answered Nov 10 at 6:18









          ssemilla

          3,077424




          3,077424












          • I tried this. But I can only get DevId. I'll read that. Thanks!
            – jecha
            Nov 10 at 6:51










          • What do you mean you "can only get DevId"? I've tried this in my local machine and it outputs your required output.
            – ssemilla
            Nov 10 at 6:59










          • The output are only values under DevId.
            – jecha
            Nov 10 at 7:11






          • 3




            OP, you have rn line endings. Use awk -v RS="rn" ... The same fixes @RavinderSingh13's answer as well.
            – James Brown
            Nov 10 at 7:15












          • @jecha, I updated the answer thanks to @JamesBrown. I would also suggest using dos2unix when working with files in in Linux if you can since most of unix tools are expecting unix line endings.
            – ssemilla
            Nov 10 at 7:21


















          • I tried this. But I can only get DevId. I'll read that. Thanks!
            – jecha
            Nov 10 at 6:51










          • What do you mean you "can only get DevId"? I've tried this in my local machine and it outputs your required output.
            – ssemilla
            Nov 10 at 6:59










          • The output are only values under DevId.
            – jecha
            Nov 10 at 7:11






          • 3




            OP, you have rn line endings. Use awk -v RS="rn" ... The same fixes @RavinderSingh13's answer as well.
            – James Brown
            Nov 10 at 7:15












          • @jecha, I updated the answer thanks to @JamesBrown. I would also suggest using dos2unix when working with files in in Linux if you can since most of unix tools are expecting unix line endings.
            – ssemilla
            Nov 10 at 7:21
















          I tried this. But I can only get DevId. I'll read that. Thanks!
          – jecha
          Nov 10 at 6:51




          I tried this. But I can only get DevId. I'll read that. Thanks!
          – jecha
          Nov 10 at 6:51












          What do you mean you "can only get DevId"? I've tried this in my local machine and it outputs your required output.
          – ssemilla
          Nov 10 at 6:59




          What do you mean you "can only get DevId"? I've tried this in my local machine and it outputs your required output.
          – ssemilla
          Nov 10 at 6:59












          The output are only values under DevId.
          – jecha
          Nov 10 at 7:11




          The output are only values under DevId.
          – jecha
          Nov 10 at 7:11




          3




          3




          OP, you have rn line endings. Use awk -v RS="rn" ... The same fixes @RavinderSingh13's answer as well.
          – James Brown
          Nov 10 at 7:15






          OP, you have rn line endings. Use awk -v RS="rn" ... The same fixes @RavinderSingh13's answer as well.
          – James Brown
          Nov 10 at 7:15














          @jecha, I updated the answer thanks to @JamesBrown. I would also suggest using dos2unix when working with files in in Linux if you can since most of unix tools are expecting unix line endings.
          – ssemilla
          Nov 10 at 7:21




          @jecha, I updated the answer thanks to @JamesBrown. I would also suggest using dos2unix when working with files in in Linux if you can since most of unix tools are expecting unix line endings.
          – ssemilla
          Nov 10 at 7:21


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236357%2fuse-awk-to-get-data-from-two-files%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini