Compare two files line by line and show only difference from file 2

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











up vote
2
down vote

favorite












I want to compare two files line by line without sorting and show only difference from file2.



file 1.txt:



one
two
three
four
five
six
seven
eight
nine
ten


file 2.txt:



one
five
three
four
five
twelve
seven
eight
hundred
ten


Then output should be



five 
twelve
hundred


I do not want to sort files.










share|improve this question









New contributor




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















  • 1




    Read man comm
    – waltinator
    2 hours ago










  • comm will complain that order is not sorted.
    – Kristopher Ives
    2 hours ago














up vote
2
down vote

favorite












I want to compare two files line by line without sorting and show only difference from file2.



file 1.txt:



one
two
three
four
five
six
seven
eight
nine
ten


file 2.txt:



one
five
three
four
five
twelve
seven
eight
hundred
ten


Then output should be



five 
twelve
hundred


I do not want to sort files.










share|improve this question









New contributor




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















  • 1




    Read man comm
    – waltinator
    2 hours ago










  • comm will complain that order is not sorted.
    – Kristopher Ives
    2 hours ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I want to compare two files line by line without sorting and show only difference from file2.



file 1.txt:



one
two
three
four
five
six
seven
eight
nine
ten


file 2.txt:



one
five
three
four
five
twelve
seven
eight
hundred
ten


Then output should be



five 
twelve
hundred


I do not want to sort files.










share|improve this question









New contributor




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











I want to compare two files line by line without sorting and show only difference from file2.



file 1.txt:



one
two
three
four
five
six
seven
eight
nine
ten


file 2.txt:



one
five
three
four
five
twelve
seven
eight
hundred
ten


Then output should be



five 
twelve
hundred


I do not want to sort files.







command-line text-processing






share|improve this question









New contributor




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











share|improve this question









New contributor




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









share|improve this question




share|improve this question








edited 1 hour ago









muru

130k19275470




130k19275470






New contributor




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









asked 3 hours ago









Vishal Patel

111




111




New contributor




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





New contributor





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






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







  • 1




    Read man comm
    – waltinator
    2 hours ago










  • comm will complain that order is not sorted.
    – Kristopher Ives
    2 hours ago












  • 1




    Read man comm
    – waltinator
    2 hours ago










  • comm will complain that order is not sorted.
    – Kristopher Ives
    2 hours ago







1




1




Read man comm
– waltinator
2 hours ago




Read man comm
– waltinator
2 hours ago












comm will complain that order is not sorted.
– Kristopher Ives
2 hours ago




comm will complain that order is not sorted.
– Kristopher Ives
2 hours ago










3 Answers
3






active

oldest

votes

















up vote
1
down vote













Doing line-by-line comparison using awk, you could do:



awk ' getline x<"file2" $0!=x print x' file1



  • getline x<"file2" reads the entire line from file2 and holds into x variable.


  • print x when line from file1 differ with line in file2.

Or same but shorter:



awk ' getline x<"file1" $0!=x' file2





share|improve this answer



























    up vote
    0
    down vote













    You cannot do this without sorting your data. Even if you never explicitly run the sort command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.






    share|improve this answer



























      up vote
      0
      down vote













      You can as well use diff for that task:



      diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt 


      Gives the following output:



      five
      twelve
      hundred





      share|improve this answer


















      • 1




        You can omit grep and do it with diff file1 file2 | awk '/>/print $2', also you could do it all with diff --old-line-format="" --unchanged-line-format="" file1 file2, so it will not print those lines. see --LTYPE-line-format=LFMT section of man diff for details.
        – Î±Ò“sнιη
        41 mins ago











      • I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
        – Videonauth
        31 mins ago










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      Vishal Patel 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%2faskubuntu.com%2fquestions%2f1079399%2fcompare-two-files-line-by-line-and-show-only-difference-from-file-2%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      Doing line-by-line comparison using awk, you could do:



      awk ' getline x<"file2" $0!=x print x' file1



      • getline x<"file2" reads the entire line from file2 and holds into x variable.


      • print x when line from file1 differ with line in file2.

      Or same but shorter:



      awk ' getline x<"file1" $0!=x' file2





      share|improve this answer
























        up vote
        1
        down vote













        Doing line-by-line comparison using awk, you could do:



        awk ' getline x<"file2" $0!=x print x' file1



        • getline x<"file2" reads the entire line from file2 and holds into x variable.


        • print x when line from file1 differ with line in file2.

        Or same but shorter:



        awk ' getline x<"file1" $0!=x' file2





        share|improve this answer






















          up vote
          1
          down vote










          up vote
          1
          down vote









          Doing line-by-line comparison using awk, you could do:



          awk ' getline x<"file2" $0!=x print x' file1



          • getline x<"file2" reads the entire line from file2 and holds into x variable.


          • print x when line from file1 differ with line in file2.

          Or same but shorter:



          awk ' getline x<"file1" $0!=x' file2





          share|improve this answer












          Doing line-by-line comparison using awk, you could do:



          awk ' getline x<"file2" $0!=x print x' file1



          • getline x<"file2" reads the entire line from file2 and holds into x variable.


          • print x when line from file1 differ with line in file2.

          Or same but shorter:



          awk ' getline x<"file1" $0!=x' file2






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          αғsнιη

          23.5k2193153




          23.5k2193153






















              up vote
              0
              down vote













              You cannot do this without sorting your data. Even if you never explicitly run the sort command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.






              share|improve this answer
























                up vote
                0
                down vote













                You cannot do this without sorting your data. Even if you never explicitly run the sort command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You cannot do this without sorting your data. Even if you never explicitly run the sort command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.






                  share|improve this answer












                  You cannot do this without sorting your data. Even if you never explicitly run the sort command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  Kristopher Ives

                  1,371613




                  1,371613




















                      up vote
                      0
                      down vote













                      You can as well use diff for that task:



                      diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt 


                      Gives the following output:



                      five
                      twelve
                      hundred





                      share|improve this answer


















                      • 1




                        You can omit grep and do it with diff file1 file2 | awk '/>/print $2', also you could do it all with diff --old-line-format="" --unchanged-line-format="" file1 file2, so it will not print those lines. see --LTYPE-line-format=LFMT section of man diff for details.
                        – Î±Ò“sнιη
                        41 mins ago











                      • I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
                        – Videonauth
                        31 mins ago














                      up vote
                      0
                      down vote













                      You can as well use diff for that task:



                      diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt 


                      Gives the following output:



                      five
                      twelve
                      hundred





                      share|improve this answer


















                      • 1




                        You can omit grep and do it with diff file1 file2 | awk '/>/print $2', also you could do it all with diff --old-line-format="" --unchanged-line-format="" file1 file2, so it will not print those lines. see --LTYPE-line-format=LFMT section of man diff for details.
                        – Î±Ò“sнιη
                        41 mins ago











                      • I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
                        – Videonauth
                        31 mins ago












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      You can as well use diff for that task:



                      diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt 


                      Gives the following output:



                      five
                      twelve
                      hundred





                      share|improve this answer














                      You can as well use diff for that task:



                      diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt 


                      Gives the following output:



                      five
                      twelve
                      hundred






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 33 mins ago

























                      answered 1 hour ago









                      Videonauth

                      22.5k116897




                      22.5k116897







                      • 1




                        You can omit grep and do it with diff file1 file2 | awk '/>/print $2', also you could do it all with diff --old-line-format="" --unchanged-line-format="" file1 file2, so it will not print those lines. see --LTYPE-line-format=LFMT section of man diff for details.
                        – Î±Ò“sнιη
                        41 mins ago











                      • I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
                        – Videonauth
                        31 mins ago












                      • 1




                        You can omit grep and do it with diff file1 file2 | awk '/>/print $2', also you could do it all with diff --old-line-format="" --unchanged-line-format="" file1 file2, so it will not print those lines. see --LTYPE-line-format=LFMT section of man diff for details.
                        – Î±Ò“sнιη
                        41 mins ago











                      • I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
                        – Videonauth
                        31 mins ago







                      1




                      1




                      You can omit grep and do it with diff file1 file2 | awk '/>/print $2', also you could do it all with diff --old-line-format="" --unchanged-line-format="" file1 file2, so it will not print those lines. see --LTYPE-line-format=LFMT section of man diff for details.
                      – Î±Ò“sнιη
                      41 mins ago





                      You can omit grep and do it with diff file1 file2 | awk '/>/print $2', also you could do it all with diff --old-line-format="" --unchanged-line-format="" file1 file2, so it will not print those lines. see --LTYPE-line-format=LFMT section of man diff for details.
                      – Î±Ò“sнιη
                      41 mins ago













                      I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
                      – Videonauth
                      31 mins ago




                      I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
                      – Videonauth
                      31 mins ago










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









                       

                      draft saved


                      draft discarded


















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












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











                      Vishal Patel 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%2faskubuntu.com%2fquestions%2f1079399%2fcompare-two-files-line-by-line-and-show-only-difference-from-file-2%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery