How to compare two different files line by line in unix? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionCompare two files and return a true or false valueUsing diff to compare the output of two commandsMerging two Unix filesCompare two files with four columnsCompare two files and matched line send to new filecompare two files and print matches - large filesScript comparing two files, match two strings anywhere on lineCompare two a columns in two csv files and print to third fileCompare two files and retrieve corresponding resultsHow to compare the 1st column of two files and if the strings match then print “true”Linux Compare two files on different field and print field 1 of first fileHow to compare two CSV files and display unique records?

.bashrc alias for a command with fixed second parameter

Why do C and C++ allow the expression (int) + 4*5;

Diophantine equation 3^a+1=3^b+5^c

3D Masyu - A Die

Is a copyright notice with a non-existent name be invalid?

By what mechanism was the 2017 UK General Election called?

Sally's older brother

Does the transliteration of 'Dravidian' exist in Hindu scripture? Does 'Dravida' refer to a Geographical area or an ethnic group?

As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?

Weaponising the Grasp-at-a-Distance spell

Adapting the Chinese Remainder Theorem (CRT) for integers to polynomials

How does the body cool itself in a stillsuit?

Does the main washing effect of soap come from foam?

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?

Any stored/leased 737s that could substitute for grounded MAXs?

How can I list files in reverse time order by a command and pass them as arguments to another command?

The Nth Gryphon Number

Short story about astronauts fertilizing soil with their own bodies

What is the proper term for etching or digging of wall to hide conduit of cables

Vertical ranges of Column Plots in 12

Is this Half dragon Quaggoth Balanced

How do I say "this must not happen"?

One-one communication

How do Java 8 default methods hеlp with lambdas?



How to compare two different files line by line in unix?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionCompare two files and return a true or false valueUsing diff to compare the output of two commandsMerging two Unix filesCompare two files with four columnsCompare two files and matched line send to new filecompare two files and print matches - large filesScript comparing two files, match two strings anywhere on lineCompare two a columns in two csv files and print to third fileCompare two files and retrieve corresponding resultsHow to compare the 1st column of two files and if the strings match then print “true”Linux Compare two files on different field and print field 1 of first fileHow to compare two CSV files and display unique records?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








12















File1:



123
234
345
456


File2:



123
234
343
758


Expected output:
File3:



TRUE
TRUE
FALSE
FALSE


so the code should compare two files and print 'TRUE' if it matches otherwise it should print 'FALSE' in the new file. Could anyone please provide the solution for this?










share|improve this question









New contributor




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















  • 10





    What happens if the two files are of unequal length? What part of the solution of this issue are you having problems with?

    – Kusalananda
    Apr 17 at 9:33






  • 9





    You might want to take a look at diff.

    – Panki
    Apr 17 at 9:36






  • 2





    Other useful command in these situations is comm. It makes it easy to list lines that both files have in common or are unique to one or the other.

    – Giacomo Alzetta
    Apr 17 at 12:12






  • 1





    @GiacomoAlzetta The thing with comm is that it requires sorted input. Apart from the fact that the example in the question does have sorted input, the question never asserts that this is the actual data that is being used and never says anything about the ordering of the data.

    – Kusalananda
    Apr 17 at 13:52






  • 2





    αғsнιη's nl trick is useful with comm for imposing sorted-ness on the files.

    – glenn jackman
    Apr 17 at 18:06

















12















File1:



123
234
345
456


File2:



123
234
343
758


Expected output:
File3:



TRUE
TRUE
FALSE
FALSE


so the code should compare two files and print 'TRUE' if it matches otherwise it should print 'FALSE' in the new file. Could anyone please provide the solution for this?










share|improve this question









New contributor




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















  • 10





    What happens if the two files are of unequal length? What part of the solution of this issue are you having problems with?

    – Kusalananda
    Apr 17 at 9:33






  • 9





    You might want to take a look at diff.

    – Panki
    Apr 17 at 9:36






  • 2





    Other useful command in these situations is comm. It makes it easy to list lines that both files have in common or are unique to one or the other.

    – Giacomo Alzetta
    Apr 17 at 12:12






  • 1





    @GiacomoAlzetta The thing with comm is that it requires sorted input. Apart from the fact that the example in the question does have sorted input, the question never asserts that this is the actual data that is being used and never says anything about the ordering of the data.

    – Kusalananda
    Apr 17 at 13:52






  • 2





    αғsнιη's nl trick is useful with comm for imposing sorted-ness on the files.

    – glenn jackman
    Apr 17 at 18:06













12












12








12


3






File1:



123
234
345
456


File2:



123
234
343
758


Expected output:
File3:



TRUE
TRUE
FALSE
FALSE


so the code should compare two files and print 'TRUE' if it matches otherwise it should print 'FALSE' in the new file. Could anyone please provide the solution for this?










share|improve this question









New contributor




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












File1:



123
234
345
456


File2:



123
234
343
758


Expected output:
File3:



TRUE
TRUE
FALSE
FALSE


so the code should compare two files and print 'TRUE' if it matches otherwise it should print 'FALSE' in the new file. Could anyone please provide the solution for this?







text-processing awk diff






share|improve this question









New contributor




Velu 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




Velu 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 Apr 17 at 10:53









αғsнιη

17.7k103270




17.7k103270






New contributor




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









asked Apr 17 at 9:28









VeluVelu

6713




6713




New contributor




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





New contributor





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






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







  • 10





    What happens if the two files are of unequal length? What part of the solution of this issue are you having problems with?

    – Kusalananda
    Apr 17 at 9:33






  • 9





    You might want to take a look at diff.

    – Panki
    Apr 17 at 9:36






  • 2





    Other useful command in these situations is comm. It makes it easy to list lines that both files have in common or are unique to one or the other.

    – Giacomo Alzetta
    Apr 17 at 12:12






  • 1





    @GiacomoAlzetta The thing with comm is that it requires sorted input. Apart from the fact that the example in the question does have sorted input, the question never asserts that this is the actual data that is being used and never says anything about the ordering of the data.

    – Kusalananda
    Apr 17 at 13:52






  • 2





    αғsнιη's nl trick is useful with comm for imposing sorted-ness on the files.

    – glenn jackman
    Apr 17 at 18:06












  • 10





    What happens if the two files are of unequal length? What part of the solution of this issue are you having problems with?

    – Kusalananda
    Apr 17 at 9:33






  • 9





    You might want to take a look at diff.

    – Panki
    Apr 17 at 9:36






  • 2





    Other useful command in these situations is comm. It makes it easy to list lines that both files have in common or are unique to one or the other.

    – Giacomo Alzetta
    Apr 17 at 12:12






  • 1





    @GiacomoAlzetta The thing with comm is that it requires sorted input. Apart from the fact that the example in the question does have sorted input, the question never asserts that this is the actual data that is being used and never says anything about the ordering of the data.

    – Kusalananda
    Apr 17 at 13:52






  • 2





    αғsнιη's nl trick is useful with comm for imposing sorted-ness on the files.

    – glenn jackman
    Apr 17 at 18:06







10




10





What happens if the two files are of unequal length? What part of the solution of this issue are you having problems with?

– Kusalananda
Apr 17 at 9:33





What happens if the two files are of unequal length? What part of the solution of this issue are you having problems with?

– Kusalananda
Apr 17 at 9:33




9




9





You might want to take a look at diff.

– Panki
Apr 17 at 9:36





You might want to take a look at diff.

– Panki
Apr 17 at 9:36




2




2





Other useful command in these situations is comm. It makes it easy to list lines that both files have in common or are unique to one or the other.

– Giacomo Alzetta
Apr 17 at 12:12





Other useful command in these situations is comm. It makes it easy to list lines that both files have in common or are unique to one or the other.

– Giacomo Alzetta
Apr 17 at 12:12




1




1





@GiacomoAlzetta The thing with comm is that it requires sorted input. Apart from the fact that the example in the question does have sorted input, the question never asserts that this is the actual data that is being used and never says anything about the ordering of the data.

– Kusalananda
Apr 17 at 13:52





@GiacomoAlzetta The thing with comm is that it requires sorted input. Apart from the fact that the example in the question does have sorted input, the question never asserts that this is the actual data that is being used and never says anything about the ordering of the data.

– Kusalananda
Apr 17 at 13:52




2




2





αғsнιη's nl trick is useful with comm for imposing sorted-ness on the files.

– glenn jackman
Apr 17 at 18:06





αғsнιη's nl trick is useful with comm for imposing sorted-ness on the files.

– glenn jackman
Apr 17 at 18:06










6 Answers
6






active

oldest

votes


















48














Use diff command as following, in bash or any other shell that supports <(...) process substitutions or you can emulate it as shown here:



diff --new-line-format='FALSE'$'n' 
--old-line-format=''
--unchanged-line-format='TRUE'$'n'
<(nl file1) <(nl file2)


Output would be:



TRUE
TRUE
FALSE
FALSE


--new-line-format='FALSE'$'n, print FALSE if lines were differ and with --old-line-format='' we disable output if line was differ for file1 which is known as old file to diff command (We could swap these as well, meaning that one of them should print FALSE another should be disabled.)



--unchanged-line-format='TRUE'$'n', print TRUE if lines were same. the $'n' C-style escaping syntax is used to printing a new line after each line output.






share|improve this answer
































    23














    Assuming the files contain no tab-characters:



    $ paste file1 file2 | awk -F 't' ' print ($1 == $2 ? "TRUE" : "FALSE") '
    TRUE
    TRUE
    FALSE
    FALSE


    This uses paste to create two tab-delimited columns, with the contents of the two files in either column. The awk command compares the two columns on each line and prints TRUE if the columns are the same and otherwise prints FALSE.






    share|improve this answer
































      10














      Assuming both files have the same number of lines:



      awk 'getline f2 < "file2"; print f2 == $0 ? "TRUE" : "FALSE"' file1


      That's doing a numerical comparison if the strings to compare are numbers and lexical otherwise. For instance, 100 and 1.0e2 would be considered identical. Change to f2"" == $0 to force a lexical comparison in any case.



      Depending on the awk implementation, lexical comparison will be done as if by using memcmp() (byte-to-byte comparison) or as if by using strcoll() (whether the two strings sort the same in the locale's collation order). That can make a difference in some locales where the order is not properly defined for some characters, not on all decimal digit input like in your sample.






      share|improve this answer
































        8














        Python 3



        with open('file1') as file1, open('file2') as file2:
        for line1, line2 in zip(file1, file2):
        print(line1 == line2)


        Output:



        True
        True
        False
        False


        If you need TRUE and FALSE in uppercase, replace the print line with one of these:



        print(str(line1 == line2).upper())
        print('TRUE' if line1 == line2 else 'FALSE')





        share|improve this answer


















        • 2





          In Python 2, do an import itertools first, and then use itertools.izip instead of zip. Otherwise it will read both files to memory, possibly using too much memory.

          – pts
          2 days ago


















        3














        In bash, reading from each file in a while loop, comparing the read lines and printing TRUE or FALSE appropriately:



        while IFS= read -r -u3 line1; IFS= read -r -u4 line2; do
        [[ $line1 == $line2 ]] && echo TRUE || echo FALSE
        done 3<file1 4<file2


        The two calls to read reads from file descriptor 3 and 4 respectively. The files are redirected to these with two input redirections into the loop.






        share|improve this answer
































          -2














          Try this,



          awk 'FNR==NR a[$0]; next if ($0 in a)print "TRUE" else print "FALSE"' File2 File1





          share|improve this answer


















          • 5





            This seems to be checking whether the first file contains each line of the second file and print TRUE if it does and FALSE if it doesn't (for each line), regardless of where in the first file the line is located. It does not compare lines line by line.

            – Kusalananda
            Apr 17 at 10:33












          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',
          autoActivateHeartbeat: false,
          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
          );



          );






          Velu 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%2f512953%2fhow-to-compare-two-different-files-line-by-line-in-unix%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          48














          Use diff command as following, in bash or any other shell that supports <(...) process substitutions or you can emulate it as shown here:



          diff --new-line-format='FALSE'$'n' 
          --old-line-format=''
          --unchanged-line-format='TRUE'$'n'
          <(nl file1) <(nl file2)


          Output would be:



          TRUE
          TRUE
          FALSE
          FALSE


          --new-line-format='FALSE'$'n, print FALSE if lines were differ and with --old-line-format='' we disable output if line was differ for file1 which is known as old file to diff command (We could swap these as well, meaning that one of them should print FALSE another should be disabled.)



          --unchanged-line-format='TRUE'$'n', print TRUE if lines were same. the $'n' C-style escaping syntax is used to printing a new line after each line output.






          share|improve this answer





























            48














            Use diff command as following, in bash or any other shell that supports <(...) process substitutions or you can emulate it as shown here:



            diff --new-line-format='FALSE'$'n' 
            --old-line-format=''
            --unchanged-line-format='TRUE'$'n'
            <(nl file1) <(nl file2)


            Output would be:



            TRUE
            TRUE
            FALSE
            FALSE


            --new-line-format='FALSE'$'n, print FALSE if lines were differ and with --old-line-format='' we disable output if line was differ for file1 which is known as old file to diff command (We could swap these as well, meaning that one of them should print FALSE another should be disabled.)



            --unchanged-line-format='TRUE'$'n', print TRUE if lines were same. the $'n' C-style escaping syntax is used to printing a new line after each line output.






            share|improve this answer



























              48












              48








              48







              Use diff command as following, in bash or any other shell that supports <(...) process substitutions or you can emulate it as shown here:



              diff --new-line-format='FALSE'$'n' 
              --old-line-format=''
              --unchanged-line-format='TRUE'$'n'
              <(nl file1) <(nl file2)


              Output would be:



              TRUE
              TRUE
              FALSE
              FALSE


              --new-line-format='FALSE'$'n, print FALSE if lines were differ and with --old-line-format='' we disable output if line was differ for file1 which is known as old file to diff command (We could swap these as well, meaning that one of them should print FALSE another should be disabled.)



              --unchanged-line-format='TRUE'$'n', print TRUE if lines were same. the $'n' C-style escaping syntax is used to printing a new line after each line output.






              share|improve this answer















              Use diff command as following, in bash or any other shell that supports <(...) process substitutions or you can emulate it as shown here:



              diff --new-line-format='FALSE'$'n' 
              --old-line-format=''
              --unchanged-line-format='TRUE'$'n'
              <(nl file1) <(nl file2)


              Output would be:



              TRUE
              TRUE
              FALSE
              FALSE


              --new-line-format='FALSE'$'n, print FALSE if lines were differ and with --old-line-format='' we disable output if line was differ for file1 which is known as old file to diff command (We could swap these as well, meaning that one of them should print FALSE another should be disabled.)



              --unchanged-line-format='TRUE'$'n', print TRUE if lines were same. the $'n' C-style escaping syntax is used to printing a new line after each line output.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 17 at 12:09









              Mathieu

              1,96711520




              1,96711520










              answered Apr 17 at 10:35









              αғsнιηαғsнιη

              17.7k103270




              17.7k103270























                  23














                  Assuming the files contain no tab-characters:



                  $ paste file1 file2 | awk -F 't' ' print ($1 == $2 ? "TRUE" : "FALSE") '
                  TRUE
                  TRUE
                  FALSE
                  FALSE


                  This uses paste to create two tab-delimited columns, with the contents of the two files in either column. The awk command compares the two columns on each line and prints TRUE if the columns are the same and otherwise prints FALSE.






                  share|improve this answer





























                    23














                    Assuming the files contain no tab-characters:



                    $ paste file1 file2 | awk -F 't' ' print ($1 == $2 ? "TRUE" : "FALSE") '
                    TRUE
                    TRUE
                    FALSE
                    FALSE


                    This uses paste to create two tab-delimited columns, with the contents of the two files in either column. The awk command compares the two columns on each line and prints TRUE if the columns are the same and otherwise prints FALSE.






                    share|improve this answer



























                      23












                      23








                      23







                      Assuming the files contain no tab-characters:



                      $ paste file1 file2 | awk -F 't' ' print ($1 == $2 ? "TRUE" : "FALSE") '
                      TRUE
                      TRUE
                      FALSE
                      FALSE


                      This uses paste to create two tab-delimited columns, with the contents of the two files in either column. The awk command compares the two columns on each line and prints TRUE if the columns are the same and otherwise prints FALSE.






                      share|improve this answer















                      Assuming the files contain no tab-characters:



                      $ paste file1 file2 | awk -F 't' ' print ($1 == $2 ? "TRUE" : "FALSE") '
                      TRUE
                      TRUE
                      FALSE
                      FALSE


                      This uses paste to create two tab-delimited columns, with the contents of the two files in either column. The awk command compares the two columns on each line and prints TRUE if the columns are the same and otherwise prints FALSE.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 18 at 5:55

























                      answered Apr 17 at 10:37









                      KusalanandaKusalananda

                      143k18267443




                      143k18267443





















                          10














                          Assuming both files have the same number of lines:



                          awk 'getline f2 < "file2"; print f2 == $0 ? "TRUE" : "FALSE"' file1


                          That's doing a numerical comparison if the strings to compare are numbers and lexical otherwise. For instance, 100 and 1.0e2 would be considered identical. Change to f2"" == $0 to force a lexical comparison in any case.



                          Depending on the awk implementation, lexical comparison will be done as if by using memcmp() (byte-to-byte comparison) or as if by using strcoll() (whether the two strings sort the same in the locale's collation order). That can make a difference in some locales where the order is not properly defined for some characters, not on all decimal digit input like in your sample.






                          share|improve this answer





























                            10














                            Assuming both files have the same number of lines:



                            awk 'getline f2 < "file2"; print f2 == $0 ? "TRUE" : "FALSE"' file1


                            That's doing a numerical comparison if the strings to compare are numbers and lexical otherwise. For instance, 100 and 1.0e2 would be considered identical. Change to f2"" == $0 to force a lexical comparison in any case.



                            Depending on the awk implementation, lexical comparison will be done as if by using memcmp() (byte-to-byte comparison) or as if by using strcoll() (whether the two strings sort the same in the locale's collation order). That can make a difference in some locales where the order is not properly defined for some characters, not on all decimal digit input like in your sample.






                            share|improve this answer



























                              10












                              10








                              10







                              Assuming both files have the same number of lines:



                              awk 'getline f2 < "file2"; print f2 == $0 ? "TRUE" : "FALSE"' file1


                              That's doing a numerical comparison if the strings to compare are numbers and lexical otherwise. For instance, 100 and 1.0e2 would be considered identical. Change to f2"" == $0 to force a lexical comparison in any case.



                              Depending on the awk implementation, lexical comparison will be done as if by using memcmp() (byte-to-byte comparison) or as if by using strcoll() (whether the two strings sort the same in the locale's collation order). That can make a difference in some locales where the order is not properly defined for some characters, not on all decimal digit input like in your sample.






                              share|improve this answer















                              Assuming both files have the same number of lines:



                              awk 'getline f2 < "file2"; print f2 == $0 ? "TRUE" : "FALSE"' file1


                              That's doing a numerical comparison if the strings to compare are numbers and lexical otherwise. For instance, 100 and 1.0e2 would be considered identical. Change to f2"" == $0 to force a lexical comparison in any case.



                              Depending on the awk implementation, lexical comparison will be done as if by using memcmp() (byte-to-byte comparison) or as if by using strcoll() (whether the two strings sort the same in the locale's collation order). That can make a difference in some locales where the order is not properly defined for some characters, not on all decimal digit input like in your sample.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Apr 17 at 12:04

























                              answered Apr 17 at 11:33









                              Stéphane ChazelasStéphane Chazelas

                              315k57598956




                              315k57598956





















                                  8














                                  Python 3



                                  with open('file1') as file1, open('file2') as file2:
                                  for line1, line2 in zip(file1, file2):
                                  print(line1 == line2)


                                  Output:



                                  True
                                  True
                                  False
                                  False


                                  If you need TRUE and FALSE in uppercase, replace the print line with one of these:



                                  print(str(line1 == line2).upper())
                                  print('TRUE' if line1 == line2 else 'FALSE')





                                  share|improve this answer


















                                  • 2





                                    In Python 2, do an import itertools first, and then use itertools.izip instead of zip. Otherwise it will read both files to memory, possibly using too much memory.

                                    – pts
                                    2 days ago















                                  8














                                  Python 3



                                  with open('file1') as file1, open('file2') as file2:
                                  for line1, line2 in zip(file1, file2):
                                  print(line1 == line2)


                                  Output:



                                  True
                                  True
                                  False
                                  False


                                  If you need TRUE and FALSE in uppercase, replace the print line with one of these:



                                  print(str(line1 == line2).upper())
                                  print('TRUE' if line1 == line2 else 'FALSE')





                                  share|improve this answer


















                                  • 2





                                    In Python 2, do an import itertools first, and then use itertools.izip instead of zip. Otherwise it will read both files to memory, possibly using too much memory.

                                    – pts
                                    2 days ago













                                  8












                                  8








                                  8







                                  Python 3



                                  with open('file1') as file1, open('file2') as file2:
                                  for line1, line2 in zip(file1, file2):
                                  print(line1 == line2)


                                  Output:



                                  True
                                  True
                                  False
                                  False


                                  If you need TRUE and FALSE in uppercase, replace the print line with one of these:



                                  print(str(line1 == line2).upper())
                                  print('TRUE' if line1 == line2 else 'FALSE')





                                  share|improve this answer













                                  Python 3



                                  with open('file1') as file1, open('file2') as file2:
                                  for line1, line2 in zip(file1, file2):
                                  print(line1 == line2)


                                  Output:



                                  True
                                  True
                                  False
                                  False


                                  If you need TRUE and FALSE in uppercase, replace the print line with one of these:



                                  print(str(line1 == line2).upper())
                                  print('TRUE' if line1 == line2 else 'FALSE')






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Apr 17 at 17:17









                                  wjandreawjandrea

                                  584414




                                  584414







                                  • 2





                                    In Python 2, do an import itertools first, and then use itertools.izip instead of zip. Otherwise it will read both files to memory, possibly using too much memory.

                                    – pts
                                    2 days ago












                                  • 2





                                    In Python 2, do an import itertools first, and then use itertools.izip instead of zip. Otherwise it will read both files to memory, possibly using too much memory.

                                    – pts
                                    2 days ago







                                  2




                                  2





                                  In Python 2, do an import itertools first, and then use itertools.izip instead of zip. Otherwise it will read both files to memory, possibly using too much memory.

                                  – pts
                                  2 days ago





                                  In Python 2, do an import itertools first, and then use itertools.izip instead of zip. Otherwise it will read both files to memory, possibly using too much memory.

                                  – pts
                                  2 days ago











                                  3














                                  In bash, reading from each file in a while loop, comparing the read lines and printing TRUE or FALSE appropriately:



                                  while IFS= read -r -u3 line1; IFS= read -r -u4 line2; do
                                  [[ $line1 == $line2 ]] && echo TRUE || echo FALSE
                                  done 3<file1 4<file2


                                  The two calls to read reads from file descriptor 3 and 4 respectively. The files are redirected to these with two input redirections into the loop.






                                  share|improve this answer





























                                    3














                                    In bash, reading from each file in a while loop, comparing the read lines and printing TRUE or FALSE appropriately:



                                    while IFS= read -r -u3 line1; IFS= read -r -u4 line2; do
                                    [[ $line1 == $line2 ]] && echo TRUE || echo FALSE
                                    done 3<file1 4<file2


                                    The two calls to read reads from file descriptor 3 and 4 respectively. The files are redirected to these with two input redirections into the loop.






                                    share|improve this answer



























                                      3












                                      3








                                      3







                                      In bash, reading from each file in a while loop, comparing the read lines and printing TRUE or FALSE appropriately:



                                      while IFS= read -r -u3 line1; IFS= read -r -u4 line2; do
                                      [[ $line1 == $line2 ]] && echo TRUE || echo FALSE
                                      done 3<file1 4<file2


                                      The two calls to read reads from file descriptor 3 and 4 respectively. The files are redirected to these with two input redirections into the loop.






                                      share|improve this answer















                                      In bash, reading from each file in a while loop, comparing the read lines and printing TRUE or FALSE appropriately:



                                      while IFS= read -r -u3 line1; IFS= read -r -u4 line2; do
                                      [[ $line1 == $line2 ]] && echo TRUE || echo FALSE
                                      done 3<file1 4<file2


                                      The two calls to read reads from file descriptor 3 and 4 respectively. The files are redirected to these with two input redirections into the loop.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Apr 17 at 19:12









                                      Kusalananda

                                      143k18267443




                                      143k18267443










                                      answered Apr 17 at 18:04









                                      glenn jackmanglenn jackman

                                      53.2k573114




                                      53.2k573114





















                                          -2














                                          Try this,



                                          awk 'FNR==NR a[$0]; next if ($0 in a)print "TRUE" else print "FALSE"' File2 File1





                                          share|improve this answer


















                                          • 5





                                            This seems to be checking whether the first file contains each line of the second file and print TRUE if it does and FALSE if it doesn't (for each line), regardless of where in the first file the line is located. It does not compare lines line by line.

                                            – Kusalananda
                                            Apr 17 at 10:33
















                                          -2














                                          Try this,



                                          awk 'FNR==NR a[$0]; next if ($0 in a)print "TRUE" else print "FALSE"' File2 File1





                                          share|improve this answer


















                                          • 5





                                            This seems to be checking whether the first file contains each line of the second file and print TRUE if it does and FALSE if it doesn't (for each line), regardless of where in the first file the line is located. It does not compare lines line by line.

                                            – Kusalananda
                                            Apr 17 at 10:33














                                          -2












                                          -2








                                          -2







                                          Try this,



                                          awk 'FNR==NR a[$0]; next if ($0 in a)print "TRUE" else print "FALSE"' File2 File1





                                          share|improve this answer













                                          Try this,



                                          awk 'FNR==NR a[$0]; next if ($0 in a)print "TRUE" else print "FALSE"' File2 File1






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Apr 17 at 10:25









                                          msp9011msp9011

                                          4,70844167




                                          4,70844167







                                          • 5





                                            This seems to be checking whether the first file contains each line of the second file and print TRUE if it does and FALSE if it doesn't (for each line), regardless of where in the first file the line is located. It does not compare lines line by line.

                                            – Kusalananda
                                            Apr 17 at 10:33













                                          • 5





                                            This seems to be checking whether the first file contains each line of the second file and print TRUE if it does and FALSE if it doesn't (for each line), regardless of where in the first file the line is located. It does not compare lines line by line.

                                            – Kusalananda
                                            Apr 17 at 10:33








                                          5




                                          5





                                          This seems to be checking whether the first file contains each line of the second file and print TRUE if it does and FALSE if it doesn't (for each line), regardless of where in the first file the line is located. It does not compare lines line by line.

                                          – Kusalananda
                                          Apr 17 at 10:33






                                          This seems to be checking whether the first file contains each line of the second file and print TRUE if it does and FALSE if it doesn't (for each line), regardless of where in the first file the line is located. It does not compare lines line by line.

                                          – Kusalananda
                                          Apr 17 at 10:33











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









                                          draft saved

                                          draft discarded


















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












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











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














                                          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.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512953%2fhow-to-compare-two-different-files-line-by-line-in-unix%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

                                          Sum ergo cogito? 1 nng

                                          三茅街道4182Guuntc Dn precexpngmageondP