I have two text files with the same number of newline terminated strings. The lines in the two files correspond to each other. The lines in each file individually might be repeated.I would like to quickly identify which two lines differ and output either from the first file or from the second one.File A:this is a stringthis is another stringempty stringFile B:this is A stringthis is another stringEmpty stringFrom perspective of file A, I would like to output first and third lines since they are different than in file B. Similarly for file B, I would output first and third lines of this files.My standard approach to comparing files was to sort the two files and then use the comm binary but sorting would destroy the correspondence between the two files. I also tried to get this done with diff but it looks to be designed for a different task.It would also be ok to output the two differing lines from both files with a tab separation.
Output different lines when comparing two files
bash;shell;sed;awk;diff
This does the comparison from the perspective of fileA:$ awk 'FNR==NR{a[NR]=$0;next;} $0!=a[FNR]' fileB fileAthis is a stringempty stringThis approach reads the whole of fileB into memory.  Thus, if your files are huge (too big for memory), you should choose another approach.Similarly, to obtain the output from the perspective of fileB:$ awk 'FNR==NR{a[NR]=$0;next;} $0!=a[FNR]' fileA fileBthis is A stringEmpty stringMore Memory Efficient ApproachThis approach only reads in two lines at a time and thus is more memory efficient.  From the perspective of FileA: $ awk '{a=$0;getline <fileA;} $0!=a' fileBthis is a stringempty stringFrom the perspective of fileB:$ awk '{a=$0;getline <fileB;} $0!=a' fileAthis is A stringEmpty string