Find string in one file and use it on another

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











up vote
0
down vote

favorite












In file 1 I need to find lines that contain string hdu in column 2, and then in file 2 replace the value in the column 2 with value of column 5 from file 1.



file1:



64 ha 1 LIG H64 64 0.000000 12.0100 c 0.637086 12.0100
65 du 1 LIG XX65 65 0.000000 16.0000 o -0.547014 16.0000
66 du 1 LIG XX66 66 0.000000 16.0000 oh -0.611114 16.0000
67 hdu 1 LIG HX67 67 0.000000 1.0080 hc 0.090186 1.0080
68 hdu 1 LIG HX68 68 0.000000 1.0080 hc 0.090186 1.0080


file2:



1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG XX67 67 2.21440000 1.72650000 2.55862000
1LIG XX68 68 2.22046000 1.85502000 2.43909000


In the given case, I need to replace XX67 with HX67, and XX68 with HX68, so that the output is:



1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG HX67 67 2.21440000 1.72650000 2.55862000
1LIG HX68 68 2.22046000 1.85502000 2.43909000


My best guess was command



awk 'FNR==NR && $2=="hdu" x=$1;y=$5 NR!=FNR if ($3==x) $2=y print' file1 file2



but it replaced only last occurrence.









share







New contributor




user318543 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    0
    down vote

    favorite












    In file 1 I need to find lines that contain string hdu in column 2, and then in file 2 replace the value in the column 2 with value of column 5 from file 1.



    file1:



    64 ha 1 LIG H64 64 0.000000 12.0100 c 0.637086 12.0100
    65 du 1 LIG XX65 65 0.000000 16.0000 o -0.547014 16.0000
    66 du 1 LIG XX66 66 0.000000 16.0000 oh -0.611114 16.0000
    67 hdu 1 LIG HX67 67 0.000000 1.0080 hc 0.090186 1.0080
    68 hdu 1 LIG HX68 68 0.000000 1.0080 hc 0.090186 1.0080


    file2:



    1LIG H64 64 2.06144000 1.94117000 2.65125000
    1LIG XX65 65 2.14737000 1.92392000 2.73722000
    1LIG XX66 66 1.98154000 2.05095000 2.64407000
    1LIG XX67 67 2.21440000 1.72650000 2.55862000
    1LIG XX68 68 2.22046000 1.85502000 2.43909000


    In the given case, I need to replace XX67 with HX67, and XX68 with HX68, so that the output is:



    1LIG H64 64 2.06144000 1.94117000 2.65125000
    1LIG XX65 65 2.14737000 1.92392000 2.73722000
    1LIG XX66 66 1.98154000 2.05095000 2.64407000
    1LIG HX67 67 2.21440000 1.72650000 2.55862000
    1LIG HX68 68 2.22046000 1.85502000 2.43909000


    My best guess was command



    awk 'FNR==NR && $2=="hdu" x=$1;y=$5 NR!=FNR if ($3==x) $2=y print' file1 file2



    but it replaced only last occurrence.









    share







    New contributor




    user318543 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      In file 1 I need to find lines that contain string hdu in column 2, and then in file 2 replace the value in the column 2 with value of column 5 from file 1.



      file1:



      64 ha 1 LIG H64 64 0.000000 12.0100 c 0.637086 12.0100
      65 du 1 LIG XX65 65 0.000000 16.0000 o -0.547014 16.0000
      66 du 1 LIG XX66 66 0.000000 16.0000 oh -0.611114 16.0000
      67 hdu 1 LIG HX67 67 0.000000 1.0080 hc 0.090186 1.0080
      68 hdu 1 LIG HX68 68 0.000000 1.0080 hc 0.090186 1.0080


      file2:



      1LIG H64 64 2.06144000 1.94117000 2.65125000
      1LIG XX65 65 2.14737000 1.92392000 2.73722000
      1LIG XX66 66 1.98154000 2.05095000 2.64407000
      1LIG XX67 67 2.21440000 1.72650000 2.55862000
      1LIG XX68 68 2.22046000 1.85502000 2.43909000


      In the given case, I need to replace XX67 with HX67, and XX68 with HX68, so that the output is:



      1LIG H64 64 2.06144000 1.94117000 2.65125000
      1LIG XX65 65 2.14737000 1.92392000 2.73722000
      1LIG XX66 66 1.98154000 2.05095000 2.64407000
      1LIG HX67 67 2.21440000 1.72650000 2.55862000
      1LIG HX68 68 2.22046000 1.85502000 2.43909000


      My best guess was command



      awk 'FNR==NR && $2=="hdu" x=$1;y=$5 NR!=FNR if ($3==x) $2=y print' file1 file2



      but it replaced only last occurrence.









      share







      New contributor




      user318543 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      In file 1 I need to find lines that contain string hdu in column 2, and then in file 2 replace the value in the column 2 with value of column 5 from file 1.



      file1:



      64 ha 1 LIG H64 64 0.000000 12.0100 c 0.637086 12.0100
      65 du 1 LIG XX65 65 0.000000 16.0000 o -0.547014 16.0000
      66 du 1 LIG XX66 66 0.000000 16.0000 oh -0.611114 16.0000
      67 hdu 1 LIG HX67 67 0.000000 1.0080 hc 0.090186 1.0080
      68 hdu 1 LIG HX68 68 0.000000 1.0080 hc 0.090186 1.0080


      file2:



      1LIG H64 64 2.06144000 1.94117000 2.65125000
      1LIG XX65 65 2.14737000 1.92392000 2.73722000
      1LIG XX66 66 1.98154000 2.05095000 2.64407000
      1LIG XX67 67 2.21440000 1.72650000 2.55862000
      1LIG XX68 68 2.22046000 1.85502000 2.43909000


      In the given case, I need to replace XX67 with HX67, and XX68 with HX68, so that the output is:



      1LIG H64 64 2.06144000 1.94117000 2.65125000
      1LIG XX65 65 2.14737000 1.92392000 2.73722000
      1LIG XX66 66 1.98154000 2.05095000 2.64407000
      1LIG HX67 67 2.21440000 1.72650000 2.55862000
      1LIG HX68 68 2.22046000 1.85502000 2.43909000


      My best guess was command



      awk 'FNR==NR && $2=="hdu" x=$1;y=$5 NR!=FNR if ($3==x) $2=y print' file1 file2



      but it replaced only last occurrence.







      awk





      share







      New contributor




      user318543 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      user318543 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      user318543 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 min ago









      user318543

      1




      1




      New contributor




      user318543 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user318543 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user318543 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























          active

          oldest

          votes











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



          );






          user318543 is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478747%2ffind-string-in-one-file-and-use-it-on-another%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          user318543 is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          user318543 is a new contributor. Be nice, and check out our Code of Conduct.












          user318543 is a new contributor. Be nice, and check out our Code of Conduct.











          user318543 is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478747%2ffind-string-in-one-file-and-use-it-on-another%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?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay