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
tenfile 2.txt:
one
five
three
four
five
twelve
seven
eight
hundred
tenThen output should be
five
twelve
hundredI do not want to sort files.
33 Answers
Doing line-by-line comparison using awk, you could do:
awk '{ getline x<"file2" } $0!=x{ print x}' file1getline 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 1 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 0 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.