-3

This question already has an answer here:

I have 2 files

1.txt

aab
aac
aad

2.txt

aaa
aab
aac

File 3 should contain

aab
aac

marked as duplicate by Sundeep, Jeff Schaller, Kiwy, Wouter Verhelst, jw013 Mar 20 '18 at 13:08

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.

  • 3
    Duplicate of like at least 3 other questions. Did you search before asking? – Marco Bonelli Mar 20 '18 at 3:02
  • Did you research before coming here to spazz? I've already tried what was mentioned in other questions. And what I asked here is different from what others have posted. – patel Frig Mar 20 '18 at 3:09
  • 1
    I did research, and I have the answer to your question, it literally takes five minutes of searching on Google. You wouldn't have had to post this question if you had researched properly. – Marco Bonelli Mar 20 '18 at 3:34
  • 2
    already tried what was mentioned in other questions please add that to the question... we do not know which question you found and what answers you tried.. – Sundeep Mar 20 '18 at 6:35
2

You can use comm (opposite of diff) for this.

comm -1 -2 1.txt 2.txt >3.txt

-1 suppresses the first column (lines that are in 1.txt but not 2.txt)

-2 suppresses the second column (lines that are in 2.txt but not 1.txt)

this leaves only the third column which will be the common lines


In Bash you can use process substitution to sort the files first:

comm -1 -2 <(sort 1.txt) <(sort 2.txt) > 3.txt
  • comm: file 2 is not in sorted order comm: file 1 is not in sorted order Is the error i get. – patel Frig Mar 20 '18 at 3:05
  • 1
    You may have to show at least some of your actual input then – Jesse_b Mar 20 '18 at 3:06
  • 1.txt user1@email.com user2@email.com user3@email.com user5@email.com noname@email.com 2.txt user1@email.com user2@email.com – patel Frig Mar 20 '18 at 3:08
  • 1
    @patelFrig, so, sort them first. – Wildcard Mar 20 '18 at 3:14

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