How to compare two different files line by line in unix?Compare two files and return a true or false valueUsing diff to compare the output of two commandsFinding out the common lines in two files using 4 fields using awk and UNIXHow can I compare two files based on the value of the first column?Merging 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 files and retrieve corresponding resultsHow to compare two CSV files and display unique records?

Generalized Assignment Problem as the sub-problem

Why didn't Snape ask Dumbledore why he let "Moody" search his office?

Rat proofing compost bin but allowing worms in

What is the next number in the series: 21, 21, 23, 20, 5, 25, 31, 24,?

How stable are PID loops really?

Would Great Old Ones care about the Blood War?

Not able to update the "ACTION" field

What was Richie's big secret?

What is the size of Neverwinter?

Why do English transliterations of Arabic names have so many Qs in them?

Electrical service sizing mix up in Mexico

Map unique raw words to a list of code words

3x3 self-descriptive squares

Why does allocating a single 2D array take longer than a loop allocating multiple 1D arrays of the same total size and shape?

Why would they pick a gamma distribution here?

Why has Donald Trump's popularity remain so stable over a rather long period of time?

What is /dev/null and why can't I use hx on it?

Should I be able to see patterns in a HS256 encoded JWT?

Meaning/translation of title "The Light Fantastic" By Terry Pratchett

Find the percentage

What if a quote contains an error

Can something have more sugar per 100g than the percentage of sugar that's in it?

What do you call the fallacy of thinking that some action A will guarantee some outcome B, when in reality B depends on multiple other conditions?

Transiting through Switzerland by coach with lots of cash



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


Compare two files and return a true or false valueUsing diff to compare the output of two commandsFinding out the common lines in two files using 4 fields using awk and UNIXHow can I compare two files based on the value of the first column?Merging 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 files and retrieve corresponding resultsHow 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;









13















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





















  • 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

















13















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





















  • 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













13












13








13


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
















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















share|improve this question













share|improve this question




share|improve this question








edited Apr 17 at 10:53









αғsнιη

19k11 gold badges35 silver badges72 bronze badges




19k11 gold badges35 silver badges72 bronze badges










asked Apr 17 at 9:28









VeluVelu

1431 silver badge7 bronze badges




1431 silver badge7 bronze badges










  • 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


















57
















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


































    25
















    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
          Apr 19 at 12:13


















        4
















        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


































          0
















          Tried with awk command and it worked fine


          awk 'NR==FNRa[$1];nextif ($1 in a)print "TRUE" elseprint "False"' file1 file2


          output



          TRUE
          TRUE
          False
          False





          share|improve this answer


























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            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/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );














            draft saved

            draft discarded
















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%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









            57
















            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































              57
















              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





























                57














                57










                57









                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

                2,0941 gold badge15 silver badges23 bronze badges




                2,0941 gold badge15 silver badges23 bronze badges










                answered Apr 17 at 10:35









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

                19k11 gold badges35 silver badges72 bronze badges




                19k11 gold badges35 silver badges72 bronze badges


























                    25
















                    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































                      25
















                      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





























                        25














                        25










                        25









                        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

                        167k20 gold badges324 silver badges519 bronze badges




                        167k20 gold badges324 silver badges519 bronze badges
























                            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

                                336k58 gold badges656 silver badges1035 bronze badges




                                336k58 gold badges656 silver badges1035 bronze badges
























                                    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
                                      Apr 19 at 12:13















                                    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
                                      Apr 19 at 12:13













                                    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

                                    6145 silver badges15 bronze badges




                                    6145 silver badges15 bronze badges










                                    • 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
                                      Apr 19 at 12:13












                                    • 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
                                      Apr 19 at 12:13







                                    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
                                    Apr 19 at 12:13





                                    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
                                    Apr 19 at 12:13











                                    4
















                                    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































                                      4
















                                      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





























                                        4














                                        4










                                        4









                                        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

                                        167k20 gold badges324 silver badges519 bronze badges




                                        167k20 gold badges324 silver badges519 bronze badges










                                        answered Apr 17 at 18:04









                                        glenn jackmanglenn jackman

                                        56.7k8 gold badges78 silver badges122 bronze badges




                                        56.7k8 gold badges78 silver badges122 bronze badges
























                                            0
















                                            Tried with awk command and it worked fine


                                            awk 'NR==FNRa[$1];nextif ($1 in a)print "TRUE" elseprint "False"' file1 file2


                                            output



                                            TRUE
                                            TRUE
                                            False
                                            False





                                            share|improve this answer





























                                              0
















                                              Tried with awk command and it worked fine


                                              awk 'NR==FNRa[$1];nextif ($1 in a)print "TRUE" elseprint "False"' file1 file2


                                              output



                                              TRUE
                                              TRUE
                                              False
                                              False





                                              share|improve this answer



























                                                0














                                                0










                                                0









                                                Tried with awk command and it worked fine


                                                awk 'NR==FNRa[$1];nextif ($1 in a)print "TRUE" elseprint "False"' file1 file2


                                                output



                                                TRUE
                                                TRUE
                                                False
                                                False





                                                share|improve this answer













                                                Tried with awk command and it worked fine


                                                awk 'NR==FNRa[$1];nextif ($1 in a)print "TRUE" elseprint "False"' file1 file2


                                                output



                                                TRUE
                                                TRUE
                                                False
                                                False






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Apr 25 at 20:40









                                                Praveen Kumar BSPraveen Kumar BS

                                                2,4082 gold badges3 silver badges11 bronze badges




                                                2,4082 gold badges3 silver badges11 bronze badges































                                                    draft saved

                                                    draft discarded















































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


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.




                                                    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

                                                    Distance measures on a map of a game The 2019 Stack Overflow Developer Survey Results Are Inmin distance in a graphShortest distance path on contour plotHow to plot a tilted map?Finding points outside of a diskDelaunay link distanceAnnulus from GeoDisks: drawing a ring on a mapNegative Correlation DistanceFind distance along a path (GPS coordinates)Finding position at given distance in a GeoPathMathematics behind distance estimation using camera

                                                    How to get a smooth, uniform ParametricPlot of a 2D Region?How to plot a complicated Region?How to exclude a region from ParametricPlotHow discretize a region placing vertices on a specific non-uniform gridHow to transform a Plot or a ParametricPlot into a RegionHow can I get a smooth plot of a bounded region?Smooth ParametricPlot3D with RegionFunction?Smooth border of a region ParametricPlotSmooth region boundarySmooth region plot from list of pointsGet minimum y of a certain x in a region

                                                    Genealogie vun de Merowenger Vum Merowech bis zum Chilperich I. | Navigatiounsmenü