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?

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

Would color changing eyes affect vision?

How to write capital alpha?

Flight departed from the gate 5 min before scheduled departure time. Refund options

Test print coming out spongy

What does it mean that physics no longer uses mechanical models to describe phenomena?

In musical terms, what properties are varied by the human voice to produce different words / syllables?

If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?

Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?

AppleTVs create a chatty alternate WiFi network

Asymptotics question

One-one communication

How can a team of shapeshifters communicate?

How can I save and copy a screenhot at the same time?

Is there public access to the Meteor Crater in Arizona?

A proverb that is used to imply that you have unexpectedly faced a big problem

What would you call this weird metallic apparatus that allows you to lift people?

How to change the tick of the color bar legend to black

Project Euler #1 in C++

Sally's older brother

what is the log of the PDF for a Normal Distribution?

Why not send Voyager 3 and 4 following up the paths taken by Voyager 1 and 2 to re-transmit signals of later as they fly away from Earth?

How to ternary Plot3D a function

What is the difference between a "ranged attack" and a "ranged weapon attack"?



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






  • 2





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

    – glenn jackman
    Apr 17 at 18:06






  • 7





    Possible duplicate of Compare two files and return a true or false value

    – Julien Lopez
    2 days ago

















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






  • 2





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

    – glenn jackman
    Apr 17 at 18:06






  • 7





    Possible duplicate of Compare two files and return a true or false value

    – Julien Lopez
    2 days ago













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






  • 2





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

    – glenn jackman
    Apr 17 at 18:06






  • 7





    Possible duplicate of Compare two files and return a true or false value

    – Julien Lopez
    2 days ago












  • 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






  • 2





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

    – glenn jackman
    Apr 17 at 18:06






  • 7





    Possible duplicate of Compare two files and return a true or false value

    – Julien Lopez
    2 days ago







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




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




7




7





Possible duplicate of Compare two files and return a true or false value

– Julien Lopez
2 days ago





Possible duplicate of Compare two files and return a true or false value

– Julien Lopez
2 days ago










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
          yesterday


















        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 2 days ago

























                      answered Apr 17 at 10:37









                      KusalanandaKusalananda

                      142k18266442




                      142k18266442





















                          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
                                    yesterday















                                  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
                                    yesterday













                                  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
                                    yesterday












                                  • 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
                                    yesterday







                                  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
                                  yesterday





                                  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
                                  yesterday











                                  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

                                      142k18266442




                                      142k18266442










                                      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

                                          How does Billy Russo acquire his 'Jigsaw' mask? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why does Bane wear the mask?Why does Kylo Ren wear a mask?Why did Captain America remove his mask while fighting Batroc the Leaper?How did the OA acquire her wisdom?Is Billy Breckenridge gay?How does Adrian Toomes hide his earnings from the IRS?What is the state of affairs on Nootka Sound by the end of season 1?How did Tia Dalma acquire Captain Barbossa's body?How is one “Deemed Worthy”, to acquire the Greatsword “Dawn”?How did Karen acquire the handgun?

                                          Личност Атрибути на личността | Литература и източници | НавигацияРаждането на личносттаредактиратередактирате

                                          A sequel to Domino's tragic life Why Christmas is for Friends Cold comfort at Charles' padSad farewell for Lady JanePS Most watched News videos