Compare Two Files Line by Line and Show Only Difference from File 2
I Want to Compare Two Files Line by Line Without Sorting and Show Only Difference from File2. File 1. Txt: One Two Three Four Five Six Seven Eight Nine Ten...
I want to compare two files line by line without sorting and show only difference from file2.
file 1.txt:
one
two
three
four
five
six
seven
eight
nine
ten
file 2.txt:
one
five
three
four
five
twelve
seven
eight
hundred
ten
Then output should be
five
twelve
hundred
I do not want to sort files.
3 Answers
Doing line-by-line comparison using awk, you could do:
awk '{ getline x<"file2" } $0!=x{ print x}' file1
getline x<"file2"reads the entire line from file2 and holds into x variable.print xwhen line from file1 differ with line in file2.
Or same but shorter:
awk '{ getline x<"file1" } $0!=x' file2
You can as well use diff for that task:
diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt
Gives the following output:
five
twelve
hundred
You cannot do this without sorting your data. Even if you never explicitly run the sort command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.