I have two files with the below data; I need the difference between two files.

I tried with diff but it also shows line which are common in the two files: (22372 Dec 4 15:36 /opt/apache-tomcat-6.0.36/webapps/new/new.txt).

First file: (multiple data exists in the same way in file 1)

22677 Dec 4 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt

22372 Dec 4 15:36 /opt/apache-tomcat-6.0.36/webapps/new/new.txt

Second file: (multiple data exists in the same way in file 2).

22372 Dec 4 15:36 /opt/apache-tomcat-6.0.36/webapps/new/new.txt

22677 Dec 3 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt

12344 Dec 10 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test.txt

I need the below output:

22677 Dec 3 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt

12344 Dec 10 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test.txt

marked as duplicate by cuonglm, jasonwryan, Anthon, Networker, garethTheRed Dec 31 '14 at 9:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @cuonglm I didn't see your answer there before. sorry if my answer is the same as yours and (deleted) – αғsнιη Dec 31 '14 at 7:11
  • 1
    @KasiyA: Don't mind and you don't need to do that. This kind of question had answered many times before. – cuonglm Dec 31 '14 at 7:23
  • I love it when someone says hey this is a duplicate then never shows in here what they are talking about and this one gets closed and no way of finding the other one so I or others can see what they were saying to learn from it. leaving them stranded, or am I just not seeing that link to the dup post? – uxserx-bw May 23 '17 at 17:08

This seems like a perfect opportunity to use comm.

$ comm -1 -3 <(sort file1) <(sort file2)
12344 Dec 10 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test.txt
22677 Dec 3 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt

-1 and -3 removes all lines unique to file 1 and all lines common to both.

Because of the sort, it will change the order of the output but that doesn't seem to be a consideration based on the question.

If the input is already sorted, you can skip the sorts yielding

$ comm -1 -3 file1 file2

Use diff -u file1 file2 | sed -nr 's/^+([^+].*)/\1/p'

Output:

22677 Dec 3 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt
12344 Dec 10 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test.txt

If you need the blank line between them, use
diff -u file1 file2 | sed -nr 's/^+([^+].*)/\1\n/p'

Output:

22677 Dec 3 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt

12344 Dec 10 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test.txt

  • I tried with this diff --suppress-common-lines file1 file2 but still it shows the common files – Sak Dec 31 '14 at 6:26
  • Updated and tested. Is this the output you need? – iyrin Dec 31 '14 at 6:36
  • When there are 2-3 lines in each file, it works fine. I have multiple lines in both files (around 4000 lines) and in that case it's not working. Same line exists in both files but not in same order and i need to exclude them in ouput. – Sak Dec 31 '14 at 6:54
  • 1
    Thanks... grep -xvFf and comm -23 <(sort file1.txt) <(sort file2.txt) resolves the issue – Sak Dec 31 '14 at 7:19
  • If lines can exist on another oder, sort the two file using sort before applying the above command – alexises Dec 31 '14 at 8:11

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