Compare columns from two files and show where the error is found












1














Compare columns 1 and 2 for File1 and File2. Stop the comparision at the fisrt time the values does not match and exit the process



File1



1 31789.00 37145.00
1 32221.00 37145.00
1 56783.00 37145.00
1 32223.00 37145.00
1 31793.00 37145.00


File2



1 31789.00 37145.00
1 32221.00 37145.00
1 31791.00 37145.00
1 32223.00 99999.00
1 31793.00 37145.00


I tried



  awk 'NR==FNR{a[$0];next}(!($0 in a)){print}' File1 File2


I don't want to show lines which does not match, I will like to stop the process on error and say the line where the error is found



Something like that:



Error found in line 3









share|improve this question



























    1














    Compare columns 1 and 2 for File1 and File2. Stop the comparision at the fisrt time the values does not match and exit the process



    File1



    1 31789.00 37145.00
    1 32221.00 37145.00
    1 56783.00 37145.00
    1 32223.00 37145.00
    1 31793.00 37145.00


    File2



    1 31789.00 37145.00
    1 32221.00 37145.00
    1 31791.00 37145.00
    1 32223.00 99999.00
    1 31793.00 37145.00


    I tried



      awk 'NR==FNR{a[$0];next}(!($0 in a)){print}' File1 File2


    I don't want to show lines which does not match, I will like to stop the process on error and say the line where the error is found



    Something like that:



    Error found in line 3









    share|improve this question

























      1












      1








      1







      Compare columns 1 and 2 for File1 and File2. Stop the comparision at the fisrt time the values does not match and exit the process



      File1



      1 31789.00 37145.00
      1 32221.00 37145.00
      1 56783.00 37145.00
      1 32223.00 37145.00
      1 31793.00 37145.00


      File2



      1 31789.00 37145.00
      1 32221.00 37145.00
      1 31791.00 37145.00
      1 32223.00 99999.00
      1 31793.00 37145.00


      I tried



        awk 'NR==FNR{a[$0];next}(!($0 in a)){print}' File1 File2


      I don't want to show lines which does not match, I will like to stop the process on error and say the line where the error is found



      Something like that:



      Error found in line 3









      share|improve this question













      Compare columns 1 and 2 for File1 and File2. Stop the comparision at the fisrt time the values does not match and exit the process



      File1



      1 31789.00 37145.00
      1 32221.00 37145.00
      1 56783.00 37145.00
      1 32223.00 37145.00
      1 31793.00 37145.00


      File2



      1 31789.00 37145.00
      1 32221.00 37145.00
      1 31791.00 37145.00
      1 32223.00 99999.00
      1 31793.00 37145.00


      I tried



        awk 'NR==FNR{a[$0];next}(!($0 in a)){print}' File1 File2


      I don't want to show lines which does not match, I will like to stop the process on error and say the line where the error is found



      Something like that:



      Error found in line 3






      awk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 16:01









      OXXO

      33929




      33929
























          3 Answers
          3






          active

          oldest

          votes


















          1














          Like this:



          awk 'NR==FNR{a[NR]=$1;b[NR]=$2;next}
          a[FNR]!=$1||b[FNR]!=$2{print "error in line "FNR; exit 1}' file1 file2





          share|improve this answer





















          • hek2mg, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03










          • You are welcome :)
            – hek2mgl
            Nov 12 '18 at 19:45



















          1














          Could you please try following. Considering that you DO NOT want to print matching lines.



          awk 'FNR==NR{a[$1,$2]=$0;next} !(($1,$2) in a){print "Error found in line "FNR;exit}'  Input_file1   Input_file2


          exit in above code will help you to come out of this execution ASAP, in case you don't need it you could remove it then from above code.



          Adding a non-one liner form of above solution too now.



          awk '                                  ##Starting awk program here.
          FNR==NR{ ##Checking condition which will be TRUE when first Input_file(Input_file1) is being read.
          a[$1,$2]=$0 ##Creating an array named a whose index is $1,$2 and value is $0.
          next ##next will skip all further statements from here.
          } ##Closing first condition block here.
          !(($1,$2) in a){ ##Checking condition if $1,$2 of 2nd Input_file is NOT in array a then do following.
          print "Error found in line "FNR ##printing error which tells ERROR not found with number of line.
          exit ##exit keyword will exit from awk prpgram.
          }
          ' Input_file1 Input_file2 ##Mentioning Input_file names Input_file1 and Input_file2 here.





          share|improve this answer























          • this won't take the order of records into consideration.
            – karakfa
            Nov 12 '18 at 16:23










          • @karakfa, thank you for your comment sir. I didn't see if OP has asked about order, if I have not missed it.
            – RavinderSingh13
            Nov 12 '18 at 16:24








          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03






          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03



















          1














          this will be simpler



          $ paste file1 file2 | awk '$1!=$4 || $2!=$5 {print "Error found in line " NR; exit 1}'
          Error found in line 3





          share|improve this answer





















          • karakfa, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:04











          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',
          autoActivateHeartbeat: false,
          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%2f53265848%2fcompare-columns-from-two-files-and-show-where-the-error-is-found%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Like this:



          awk 'NR==FNR{a[NR]=$1;b[NR]=$2;next}
          a[FNR]!=$1||b[FNR]!=$2{print "error in line "FNR; exit 1}' file1 file2





          share|improve this answer





















          • hek2mg, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03










          • You are welcome :)
            – hek2mgl
            Nov 12 '18 at 19:45
















          1














          Like this:



          awk 'NR==FNR{a[NR]=$1;b[NR]=$2;next}
          a[FNR]!=$1||b[FNR]!=$2{print "error in line "FNR; exit 1}' file1 file2





          share|improve this answer





















          • hek2mg, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03










          • You are welcome :)
            – hek2mgl
            Nov 12 '18 at 19:45














          1












          1








          1






          Like this:



          awk 'NR==FNR{a[NR]=$1;b[NR]=$2;next}
          a[FNR]!=$1||b[FNR]!=$2{print "error in line "FNR; exit 1}' file1 file2





          share|improve this answer












          Like this:



          awk 'NR==FNR{a[NR]=$1;b[NR]=$2;next}
          a[FNR]!=$1||b[FNR]!=$2{print "error in line "FNR; exit 1}' file1 file2






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 16:09









          hek2mgl

          106k13142164




          106k13142164












          • hek2mg, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03










          • You are welcome :)
            – hek2mgl
            Nov 12 '18 at 19:45


















          • hek2mg, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03










          • You are welcome :)
            – hek2mgl
            Nov 12 '18 at 19:45
















          hek2mg, Thanks a lot for the code, works perfectly
          – OXXO
          Nov 12 '18 at 18:03




          hek2mg, Thanks a lot for the code, works perfectly
          – OXXO
          Nov 12 '18 at 18:03












          You are welcome :)
          – hek2mgl
          Nov 12 '18 at 19:45




          You are welcome :)
          – hek2mgl
          Nov 12 '18 at 19:45













          1














          Could you please try following. Considering that you DO NOT want to print matching lines.



          awk 'FNR==NR{a[$1,$2]=$0;next} !(($1,$2) in a){print "Error found in line "FNR;exit}'  Input_file1   Input_file2


          exit in above code will help you to come out of this execution ASAP, in case you don't need it you could remove it then from above code.



          Adding a non-one liner form of above solution too now.



          awk '                                  ##Starting awk program here.
          FNR==NR{ ##Checking condition which will be TRUE when first Input_file(Input_file1) is being read.
          a[$1,$2]=$0 ##Creating an array named a whose index is $1,$2 and value is $0.
          next ##next will skip all further statements from here.
          } ##Closing first condition block here.
          !(($1,$2) in a){ ##Checking condition if $1,$2 of 2nd Input_file is NOT in array a then do following.
          print "Error found in line "FNR ##printing error which tells ERROR not found with number of line.
          exit ##exit keyword will exit from awk prpgram.
          }
          ' Input_file1 Input_file2 ##Mentioning Input_file names Input_file1 and Input_file2 here.





          share|improve this answer























          • this won't take the order of records into consideration.
            – karakfa
            Nov 12 '18 at 16:23










          • @karakfa, thank you for your comment sir. I didn't see if OP has asked about order, if I have not missed it.
            – RavinderSingh13
            Nov 12 '18 at 16:24








          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03






          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03
















          1














          Could you please try following. Considering that you DO NOT want to print matching lines.



          awk 'FNR==NR{a[$1,$2]=$0;next} !(($1,$2) in a){print "Error found in line "FNR;exit}'  Input_file1   Input_file2


          exit in above code will help you to come out of this execution ASAP, in case you don't need it you could remove it then from above code.



          Adding a non-one liner form of above solution too now.



          awk '                                  ##Starting awk program here.
          FNR==NR{ ##Checking condition which will be TRUE when first Input_file(Input_file1) is being read.
          a[$1,$2]=$0 ##Creating an array named a whose index is $1,$2 and value is $0.
          next ##next will skip all further statements from here.
          } ##Closing first condition block here.
          !(($1,$2) in a){ ##Checking condition if $1,$2 of 2nd Input_file is NOT in array a then do following.
          print "Error found in line "FNR ##printing error which tells ERROR not found with number of line.
          exit ##exit keyword will exit from awk prpgram.
          }
          ' Input_file1 Input_file2 ##Mentioning Input_file names Input_file1 and Input_file2 here.





          share|improve this answer























          • this won't take the order of records into consideration.
            – karakfa
            Nov 12 '18 at 16:23










          • @karakfa, thank you for your comment sir. I didn't see if OP has asked about order, if I have not missed it.
            – RavinderSingh13
            Nov 12 '18 at 16:24








          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03






          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03














          1












          1








          1






          Could you please try following. Considering that you DO NOT want to print matching lines.



          awk 'FNR==NR{a[$1,$2]=$0;next} !(($1,$2) in a){print "Error found in line "FNR;exit}'  Input_file1   Input_file2


          exit in above code will help you to come out of this execution ASAP, in case you don't need it you could remove it then from above code.



          Adding a non-one liner form of above solution too now.



          awk '                                  ##Starting awk program here.
          FNR==NR{ ##Checking condition which will be TRUE when first Input_file(Input_file1) is being read.
          a[$1,$2]=$0 ##Creating an array named a whose index is $1,$2 and value is $0.
          next ##next will skip all further statements from here.
          } ##Closing first condition block here.
          !(($1,$2) in a){ ##Checking condition if $1,$2 of 2nd Input_file is NOT in array a then do following.
          print "Error found in line "FNR ##printing error which tells ERROR not found with number of line.
          exit ##exit keyword will exit from awk prpgram.
          }
          ' Input_file1 Input_file2 ##Mentioning Input_file names Input_file1 and Input_file2 here.





          share|improve this answer














          Could you please try following. Considering that you DO NOT want to print matching lines.



          awk 'FNR==NR{a[$1,$2]=$0;next} !(($1,$2) in a){print "Error found in line "FNR;exit}'  Input_file1   Input_file2


          exit in above code will help you to come out of this execution ASAP, in case you don't need it you could remove it then from above code.



          Adding a non-one liner form of above solution too now.



          awk '                                  ##Starting awk program here.
          FNR==NR{ ##Checking condition which will be TRUE when first Input_file(Input_file1) is being read.
          a[$1,$2]=$0 ##Creating an array named a whose index is $1,$2 and value is $0.
          next ##next will skip all further statements from here.
          } ##Closing first condition block here.
          !(($1,$2) in a){ ##Checking condition if $1,$2 of 2nd Input_file is NOT in array a then do following.
          print "Error found in line "FNR ##printing error which tells ERROR not found with number of line.
          exit ##exit keyword will exit from awk prpgram.
          }
          ' Input_file1 Input_file2 ##Mentioning Input_file names Input_file1 and Input_file2 here.






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 16:16

























          answered Nov 12 '18 at 16:05









          RavinderSingh13

          25.7k41438




          25.7k41438












          • this won't take the order of records into consideration.
            – karakfa
            Nov 12 '18 at 16:23










          • @karakfa, thank you for your comment sir. I didn't see if OP has asked about order, if I have not missed it.
            – RavinderSingh13
            Nov 12 '18 at 16:24








          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03






          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03


















          • this won't take the order of records into consideration.
            – karakfa
            Nov 12 '18 at 16:23










          • @karakfa, thank you for your comment sir. I didn't see if OP has asked about order, if I have not missed it.
            – RavinderSingh13
            Nov 12 '18 at 16:24








          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03






          • 1




            Ravinder, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:03
















          this won't take the order of records into consideration.
          – karakfa
          Nov 12 '18 at 16:23




          this won't take the order of records into consideration.
          – karakfa
          Nov 12 '18 at 16:23












          @karakfa, thank you for your comment sir. I didn't see if OP has asked about order, if I have not missed it.
          – RavinderSingh13
          Nov 12 '18 at 16:24






          @karakfa, thank you for your comment sir. I didn't see if OP has asked about order, if I have not missed it.
          – RavinderSingh13
          Nov 12 '18 at 16:24






          1




          1




          Ravinder, Thanks a lot for the code, works perfectly
          – OXXO
          Nov 12 '18 at 18:03




          Ravinder, Thanks a lot for the code, works perfectly
          – OXXO
          Nov 12 '18 at 18:03




          1




          1




          Ravinder, Thanks a lot for the code, works perfectly
          – OXXO
          Nov 12 '18 at 18:03




          Ravinder, Thanks a lot for the code, works perfectly
          – OXXO
          Nov 12 '18 at 18:03











          1














          this will be simpler



          $ paste file1 file2 | awk '$1!=$4 || $2!=$5 {print "Error found in line " NR; exit 1}'
          Error found in line 3





          share|improve this answer





















          • karakfa, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:04
















          1














          this will be simpler



          $ paste file1 file2 | awk '$1!=$4 || $2!=$5 {print "Error found in line " NR; exit 1}'
          Error found in line 3





          share|improve this answer





















          • karakfa, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:04














          1












          1








          1






          this will be simpler



          $ paste file1 file2 | awk '$1!=$4 || $2!=$5 {print "Error found in line " NR; exit 1}'
          Error found in line 3





          share|improve this answer












          this will be simpler



          $ paste file1 file2 | awk '$1!=$4 || $2!=$5 {print "Error found in line " NR; exit 1}'
          Error found in line 3






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 16:19









          karakfa

          48.2k52738




          48.2k52738












          • karakfa, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:04


















          • karakfa, Thanks a lot for the code, works perfectly
            – OXXO
            Nov 12 '18 at 18:04
















          karakfa, Thanks a lot for the code, works perfectly
          – OXXO
          Nov 12 '18 at 18:04




          karakfa, Thanks a lot for the code, works perfectly
          – OXXO
          Nov 12 '18 at 18:04


















          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%2f53265848%2fcompare-columns-from-two-files-and-show-where-the-error-is-found%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







          Popular posts from this blog

          The Sandy Post

          Danny Elfman

          Pages that link to "Head v. Amoskeag Manufacturing Co."