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?

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

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.

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.

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')
  • 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.

0
Tried with awk command and it worked fine


awk 'NR==FNR{a[$1];next}{if ($1 in a){print "TRUE"} else{print "False"}}' file1 file2

output

TRUE
TRUE
False
False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.