How to get character level difference using “diff” command in Linux using shell script?











up vote
2
down vote

favorite
1












I have two SQL files, one is old.sql and the other one is new.sql.



Suppose old.sql contains a table with three fields, Emp_Id, Name and Address and data stored in old.sql as follows:



Insert into table1 values (101 ,"a", "xyz");
Insert into table1 values (102 ,"b", "pqr");


Then I have changed "a" address "xyz" to "xyz123" and saved that data in the new.sql file.
Now the new.sql file contains data as follows:



Insert into table1 values (101 ,"a", "xyz123");
Insert into table1 values (102 ,"b", "pqr");


When I use the diff command like this:



diff old.sql new.sql


it gives differences line-wise but I want only updated data, like xyz123.










share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    I have two SQL files, one is old.sql and the other one is new.sql.



    Suppose old.sql contains a table with three fields, Emp_Id, Name and Address and data stored in old.sql as follows:



    Insert into table1 values (101 ,"a", "xyz");
    Insert into table1 values (102 ,"b", "pqr");


    Then I have changed "a" address "xyz" to "xyz123" and saved that data in the new.sql file.
    Now the new.sql file contains data as follows:



    Insert into table1 values (101 ,"a", "xyz123");
    Insert into table1 values (102 ,"b", "pqr");


    When I use the diff command like this:



    diff old.sql new.sql


    it gives differences line-wise but I want only updated data, like xyz123.










    share|improve this question


























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I have two SQL files, one is old.sql and the other one is new.sql.



      Suppose old.sql contains a table with three fields, Emp_Id, Name and Address and data stored in old.sql as follows:



      Insert into table1 values (101 ,"a", "xyz");
      Insert into table1 values (102 ,"b", "pqr");


      Then I have changed "a" address "xyz" to "xyz123" and saved that data in the new.sql file.
      Now the new.sql file contains data as follows:



      Insert into table1 values (101 ,"a", "xyz123");
      Insert into table1 values (102 ,"b", "pqr");


      When I use the diff command like this:



      diff old.sql new.sql


      it gives differences line-wise but I want only updated data, like xyz123.










      share|improve this question















      I have two SQL files, one is old.sql and the other one is new.sql.



      Suppose old.sql contains a table with three fields, Emp_Id, Name and Address and data stored in old.sql as follows:



      Insert into table1 values (101 ,"a", "xyz");
      Insert into table1 values (102 ,"b", "pqr");


      Then I have changed "a" address "xyz" to "xyz123" and saved that data in the new.sql file.
      Now the new.sql file contains data as follows:



      Insert into table1 values (101 ,"a", "xyz123");
      Insert into table1 values (102 ,"b", "pqr");


      When I use the diff command like this:



      diff old.sql new.sql


      it gives differences line-wise but I want only updated data, like xyz123.







      shell-script diff






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 31 '16 at 9:04









      Stephen Kitt

      162k24358436




      162k24358436










      asked May 31 '16 at 7:28









      user168519

      1415




      1415






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote













          You might find wdiff useful for this type of comparison; it's a front-end to diff which produces word-by-word comparisons. With your example it produces by default



          Insert into table1 values (101 ,"a", [-"xyz");-] {+"xyz123");+}
          Insert into table1 values (102 ,"b", "pqr");


          It can use terminal features to make the output more legible on a terminal (wdiff -t). It also has a -3 option which limits output to changed words only:



          ======================================================================
          [-"xyz");-] {+"xyz123");+}
          ======================================================================


          If you don't have wdiff already installed, you need to install it. Run sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or the command that is appropriate to your operating system.






          share|improve this answer



















          • 1




            but when I use wdiff command instead of diff command prompt is showing error wdiff command not found
            – user168519
            Jun 2 '16 at 12:16












          • @user168519 that simply means you need to install it, sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or your local equivalent.
            – Stephen Kitt
            Jun 2 '16 at 12:21










          • Hi Stephen, I have installed wdiff but it is not comparing data present inside the parenthesis.
            – user168519
            Jun 2 '16 at 13:35










          • @user168519 that's odd, could you edit your question to show wdiff's behaviour?
            – Stephen Kitt
            Jun 2 '16 at 13:58










          • Okay I will edit my question, Thanks.
            – user168519
            Jun 3 '16 at 5:36




















          up vote
          1
          down vote













          By definition, diff is showing differences lines by lines (see diff manual page), it will therefore not show only the differing characters.
          You can reduce the amount of difference by pre-processing the files, for exemple by inserting an end-of-line character after each semi-column:



          sed -e 's/;/;'$'n/g' old.sql > old.patched


          Then use diff on the two resulting files.






          share|improve this answer




























            up vote
            1
            down vote













            You can use:



            diff -u old.sql new.sql |colordiff |diff-highlight


            screenshot



            colordiff is a Ubuntu package. You can install it using sudo apt-get install colordiff.



            diff-hight is from git (since version 2.9). It is located in /usr/share/doc/git/contrib/diff-highlight/diff-highlight. You can put it somewhere in your $PATH. Or get it from diff-so-fancy project.






            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: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2funix.stackexchange.com%2fquestions%2f286601%2fhow-to-get-character-level-difference-using-diff-command-in-linux-using-shell%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








              up vote
              4
              down vote













              You might find wdiff useful for this type of comparison; it's a front-end to diff which produces word-by-word comparisons. With your example it produces by default



              Insert into table1 values (101 ,"a", [-"xyz");-] {+"xyz123");+}
              Insert into table1 values (102 ,"b", "pqr");


              It can use terminal features to make the output more legible on a terminal (wdiff -t). It also has a -3 option which limits output to changed words only:



              ======================================================================
              [-"xyz");-] {+"xyz123");+}
              ======================================================================


              If you don't have wdiff already installed, you need to install it. Run sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or the command that is appropriate to your operating system.






              share|improve this answer



















              • 1




                but when I use wdiff command instead of diff command prompt is showing error wdiff command not found
                – user168519
                Jun 2 '16 at 12:16












              • @user168519 that simply means you need to install it, sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or your local equivalent.
                – Stephen Kitt
                Jun 2 '16 at 12:21










              • Hi Stephen, I have installed wdiff but it is not comparing data present inside the parenthesis.
                – user168519
                Jun 2 '16 at 13:35










              • @user168519 that's odd, could you edit your question to show wdiff's behaviour?
                – Stephen Kitt
                Jun 2 '16 at 13:58










              • Okay I will edit my question, Thanks.
                – user168519
                Jun 3 '16 at 5:36

















              up vote
              4
              down vote













              You might find wdiff useful for this type of comparison; it's a front-end to diff which produces word-by-word comparisons. With your example it produces by default



              Insert into table1 values (101 ,"a", [-"xyz");-] {+"xyz123");+}
              Insert into table1 values (102 ,"b", "pqr");


              It can use terminal features to make the output more legible on a terminal (wdiff -t). It also has a -3 option which limits output to changed words only:



              ======================================================================
              [-"xyz");-] {+"xyz123");+}
              ======================================================================


              If you don't have wdiff already installed, you need to install it. Run sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or the command that is appropriate to your operating system.






              share|improve this answer



















              • 1




                but when I use wdiff command instead of diff command prompt is showing error wdiff command not found
                – user168519
                Jun 2 '16 at 12:16












              • @user168519 that simply means you need to install it, sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or your local equivalent.
                – Stephen Kitt
                Jun 2 '16 at 12:21










              • Hi Stephen, I have installed wdiff but it is not comparing data present inside the parenthesis.
                – user168519
                Jun 2 '16 at 13:35










              • @user168519 that's odd, could you edit your question to show wdiff's behaviour?
                – Stephen Kitt
                Jun 2 '16 at 13:58










              • Okay I will edit my question, Thanks.
                – user168519
                Jun 3 '16 at 5:36















              up vote
              4
              down vote










              up vote
              4
              down vote









              You might find wdiff useful for this type of comparison; it's a front-end to diff which produces word-by-word comparisons. With your example it produces by default



              Insert into table1 values (101 ,"a", [-"xyz");-] {+"xyz123");+}
              Insert into table1 values (102 ,"b", "pqr");


              It can use terminal features to make the output more legible on a terminal (wdiff -t). It also has a -3 option which limits output to changed words only:



              ======================================================================
              [-"xyz");-] {+"xyz123");+}
              ======================================================================


              If you don't have wdiff already installed, you need to install it. Run sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or the command that is appropriate to your operating system.






              share|improve this answer














              You might find wdiff useful for this type of comparison; it's a front-end to diff which produces word-by-word comparisons. With your example it produces by default



              Insert into table1 values (101 ,"a", [-"xyz");-] {+"xyz123");+}
              Insert into table1 values (102 ,"b", "pqr");


              It can use terminal features to make the output more legible on a terminal (wdiff -t). It also has a -3 option which limits output to changed words only:



              ======================================================================
              [-"xyz");-] {+"xyz123");+}
              ======================================================================


              If you don't have wdiff already installed, you need to install it. Run sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or the command that is appropriate to your operating system.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited yesterday









              Prasanna

              993




              993










              answered May 31 '16 at 9:01









              Stephen Kitt

              162k24358436




              162k24358436








              • 1




                but when I use wdiff command instead of diff command prompt is showing error wdiff command not found
                – user168519
                Jun 2 '16 at 12:16












              • @user168519 that simply means you need to install it, sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or your local equivalent.
                – Stephen Kitt
                Jun 2 '16 at 12:21










              • Hi Stephen, I have installed wdiff but it is not comparing data present inside the parenthesis.
                – user168519
                Jun 2 '16 at 13:35










              • @user168519 that's odd, could you edit your question to show wdiff's behaviour?
                – Stephen Kitt
                Jun 2 '16 at 13:58










              • Okay I will edit my question, Thanks.
                – user168519
                Jun 3 '16 at 5:36
















              • 1




                but when I use wdiff command instead of diff command prompt is showing error wdiff command not found
                – user168519
                Jun 2 '16 at 12:16












              • @user168519 that simply means you need to install it, sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or your local equivalent.
                – Stephen Kitt
                Jun 2 '16 at 12:21










              • Hi Stephen, I have installed wdiff but it is not comparing data present inside the parenthesis.
                – user168519
                Jun 2 '16 at 13:35










              • @user168519 that's odd, could you edit your question to show wdiff's behaviour?
                – Stephen Kitt
                Jun 2 '16 at 13:58










              • Okay I will edit my question, Thanks.
                – user168519
                Jun 3 '16 at 5:36










              1




              1




              but when I use wdiff command instead of diff command prompt is showing error wdiff command not found
              – user168519
              Jun 2 '16 at 12:16






              but when I use wdiff command instead of diff command prompt is showing error wdiff command not found
              – user168519
              Jun 2 '16 at 12:16














              @user168519 that simply means you need to install it, sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or your local equivalent.
              – Stephen Kitt
              Jun 2 '16 at 12:21




              @user168519 that simply means you need to install it, sudo apt-get install wdiff or sudo dnf install wdiff or sudo yum install wdiff or your local equivalent.
              – Stephen Kitt
              Jun 2 '16 at 12:21












              Hi Stephen, I have installed wdiff but it is not comparing data present inside the parenthesis.
              – user168519
              Jun 2 '16 at 13:35




              Hi Stephen, I have installed wdiff but it is not comparing data present inside the parenthesis.
              – user168519
              Jun 2 '16 at 13:35












              @user168519 that's odd, could you edit your question to show wdiff's behaviour?
              – Stephen Kitt
              Jun 2 '16 at 13:58




              @user168519 that's odd, could you edit your question to show wdiff's behaviour?
              – Stephen Kitt
              Jun 2 '16 at 13:58












              Okay I will edit my question, Thanks.
              – user168519
              Jun 3 '16 at 5:36






              Okay I will edit my question, Thanks.
              – user168519
              Jun 3 '16 at 5:36














              up vote
              1
              down vote













              By definition, diff is showing differences lines by lines (see diff manual page), it will therefore not show only the differing characters.
              You can reduce the amount of difference by pre-processing the files, for exemple by inserting an end-of-line character after each semi-column:



              sed -e 's/;/;'$'n/g' old.sql > old.patched


              Then use diff on the two resulting files.






              share|improve this answer

























                up vote
                1
                down vote













                By definition, diff is showing differences lines by lines (see diff manual page), it will therefore not show only the differing characters.
                You can reduce the amount of difference by pre-processing the files, for exemple by inserting an end-of-line character after each semi-column:



                sed -e 's/;/;'$'n/g' old.sql > old.patched


                Then use diff on the two resulting files.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  By definition, diff is showing differences lines by lines (see diff manual page), it will therefore not show only the differing characters.
                  You can reduce the amount of difference by pre-processing the files, for exemple by inserting an end-of-line character after each semi-column:



                  sed -e 's/;/;'$'n/g' old.sql > old.patched


                  Then use diff on the two resulting files.






                  share|improve this answer












                  By definition, diff is showing differences lines by lines (see diff manual page), it will therefore not show only the differing characters.
                  You can reduce the amount of difference by pre-processing the files, for exemple by inserting an end-of-line character after each semi-column:



                  sed -e 's/;/;'$'n/g' old.sql > old.patched


                  Then use diff on the two resulting files.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 31 '16 at 8:03









                  PierreL

                  764




                  764






















                      up vote
                      1
                      down vote













                      You can use:



                      diff -u old.sql new.sql |colordiff |diff-highlight


                      screenshot



                      colordiff is a Ubuntu package. You can install it using sudo apt-get install colordiff.



                      diff-hight is from git (since version 2.9). It is located in /usr/share/doc/git/contrib/diff-highlight/diff-highlight. You can put it somewhere in your $PATH. Or get it from diff-so-fancy project.






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        You can use:



                        diff -u old.sql new.sql |colordiff |diff-highlight


                        screenshot



                        colordiff is a Ubuntu package. You can install it using sudo apt-get install colordiff.



                        diff-hight is from git (since version 2.9). It is located in /usr/share/doc/git/contrib/diff-highlight/diff-highlight. You can put it somewhere in your $PATH. Or get it from diff-so-fancy project.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          You can use:



                          diff -u old.sql new.sql |colordiff |diff-highlight


                          screenshot



                          colordiff is a Ubuntu package. You can install it using sudo apt-get install colordiff.



                          diff-hight is from git (since version 2.9). It is located in /usr/share/doc/git/contrib/diff-highlight/diff-highlight. You can put it somewhere in your $PATH. Or get it from diff-so-fancy project.






                          share|improve this answer












                          You can use:



                          diff -u old.sql new.sql |colordiff |diff-highlight


                          screenshot



                          colordiff is a Ubuntu package. You can install it using sudo apt-get install colordiff.



                          diff-hight is from git (since version 2.9). It is located in /usr/share/doc/git/contrib/diff-highlight/diff-highlight. You can put it somewhere in your $PATH. Or get it from diff-so-fancy project.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Oct 5 '16 at 15:51









                          zhanxw

                          17613




                          17613






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • 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%2funix.stackexchange.com%2fquestions%2f286601%2fhow-to-get-character-level-difference-using-diff-command-in-linux-using-shell%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

                              Accessing regular linux commands in Huawei's Dopra Linux

                              Can't connect RFCOMM socket: Host is down

                              Kernel panic - not syncing: Fatal Exception in Interrupt