Glam Prestige Journal

Bright entertainment trends with youth appeal.

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

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 x when 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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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