Comparing Data between 2 different files in Unix

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











up vote
0
down vote

favorite












I have 2 different files-



File 1



2
4
6
8
10
12


File 2



2
3
5
6
10
12


I want to compare 2 files and get the output data which is in File 1 but not in File 2-



Output



4
8


I am using below command but not getting desired output-




comm -23 file1 file2








share|improve this question



















  • I think the problem is comm expects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically, 1, 10, and 12 all need to sort before 2, so you need to resort your file1 and file2 to use comm.
    – jw013
    Jun 13 at 15:34






  • 1




    You'd be getting an error from comm. It's always important to share these in your question.
    – roaima
    Jun 13 at 19:07














up vote
0
down vote

favorite












I have 2 different files-



File 1



2
4
6
8
10
12


File 2



2
3
5
6
10
12


I want to compare 2 files and get the output data which is in File 1 but not in File 2-



Output



4
8


I am using below command but not getting desired output-




comm -23 file1 file2








share|improve this question



















  • I think the problem is comm expects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically, 1, 10, and 12 all need to sort before 2, so you need to resort your file1 and file2 to use comm.
    – jw013
    Jun 13 at 15:34






  • 1




    You'd be getting an error from comm. It's always important to share these in your question.
    – roaima
    Jun 13 at 19:07












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have 2 different files-



File 1



2
4
6
8
10
12


File 2



2
3
5
6
10
12


I want to compare 2 files and get the output data which is in File 1 but not in File 2-



Output



4
8


I am using below command but not getting desired output-




comm -23 file1 file2








share|improve this question











I have 2 different files-



File 1



2
4
6
8
10
12


File 2



2
3
5
6
10
12


I want to compare 2 files and get the output data which is in File 1 but not in File 2-



Output



4
8


I am using below command but not getting desired output-




comm -23 file1 file2










share|improve this question










share|improve this question




share|improve this question









asked Jun 13 at 15:26









Praveen Verma

2316




2316











  • I think the problem is comm expects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically, 1, 10, and 12 all need to sort before 2, so you need to resort your file1 and file2 to use comm.
    – jw013
    Jun 13 at 15:34






  • 1




    You'd be getting an error from comm. It's always important to share these in your question.
    – roaima
    Jun 13 at 19:07
















  • I think the problem is comm expects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically, 1, 10, and 12 all need to sort before 2, so you need to resort your file1 and file2 to use comm.
    – jw013
    Jun 13 at 15:34






  • 1




    You'd be getting an error from comm. It's always important to share these in your question.
    – roaima
    Jun 13 at 19:07















I think the problem is comm expects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically, 1, 10, and 12 all need to sort before 2, so you need to resort your file1 and file2 to use comm.
– jw013
Jun 13 at 15:34




I think the problem is comm expects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically, 1, 10, and 12 all need to sort before 2, so you need to resort your file1 and file2 to use comm.
– jw013
Jun 13 at 15:34




1




1




You'd be getting an error from comm. It's always important to share these in your question.
– roaima
Jun 13 at 19:07




You'd be getting an error from comm. It's always important to share these in your question.
– roaima
Jun 13 at 19:07










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using



sort -o file1 file1
sort -o file2 file2


Then:



$ comm -23 file1 file2
4
8


Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:



$ comm -23 <( sort file1 ) <( sort file2 )
4
8





share|improve this answer




























    up vote
    2
    down vote













    You can use grep



    grep -F -x -f 'File2' -v 'File1'


    Pattern of fixed--strings (-F) in File2 (-f)



    Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.



    find similar line and inverse with (-v)






    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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f449578%2fcomparing-data-between-2-different-files-in-unix%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using



      sort -o file1 file1
      sort -o file2 file2


      Then:



      $ comm -23 file1 file2
      4
      8


      Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:



      $ comm -23 <( sort file1 ) <( sort file2 )
      4
      8





      share|improve this answer

























        up vote
        4
        down vote



        accepted










        For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using



        sort -o file1 file1
        sort -o file2 file2


        Then:



        $ comm -23 file1 file2
        4
        8


        Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:



        $ comm -23 <( sort file1 ) <( sort file2 )
        4
        8





        share|improve this answer























          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using



          sort -o file1 file1
          sort -o file2 file2


          Then:



          $ comm -23 file1 file2
          4
          8


          Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:



          $ comm -23 <( sort file1 ) <( sort file2 )
          4
          8





          share|improve this answer













          For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using



          sort -o file1 file1
          sort -o file2 file2


          Then:



          $ comm -23 file1 file2
          4
          8


          Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:



          $ comm -23 <( sort file1 ) <( sort file2 )
          4
          8






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 13 at 15:36









          Kusalananda

          101k13199312




          101k13199312






















              up vote
              2
              down vote













              You can use grep



              grep -F -x -f 'File2' -v 'File1'


              Pattern of fixed--strings (-F) in File2 (-f)



              Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.



              find similar line and inverse with (-v)






              share|improve this answer



























                up vote
                2
                down vote













                You can use grep



                grep -F -x -f 'File2' -v 'File1'


                Pattern of fixed--strings (-F) in File2 (-f)



                Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.



                find similar line and inverse with (-v)






                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  You can use grep



                  grep -F -x -f 'File2' -v 'File1'


                  Pattern of fixed--strings (-F) in File2 (-f)



                  Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.



                  find similar line and inverse with (-v)






                  share|improve this answer















                  You can use grep



                  grep -F -x -f 'File2' -v 'File1'


                  Pattern of fixed--strings (-F) in File2 (-f)



                  Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.



                  find similar line and inverse with (-v)







                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Jun 13 at 15:53









                  glenn jackman

                  45.6k265100




                  45.6k265100











                  answered Jun 13 at 15:46









                  ctac_

                  996116




                  996116






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449578%2fcomparing-data-between-2-different-files-in-unix%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?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?