Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two text files for the same exact text using BASH?

Let's say I have two text files that I need to extract data out of. The text of the two files is as follows:

File 1:

1name - [email protected]
2Name - [email protected]
3Name - [email protected]
4Name - [email protected]

File 2:

email.com
email.com
email.com
anotherwebsite.com

File 2 is File 1's list of domain names, extracted from the email addresses. These are not the same domain names by any means, and are quite random.

How can I get the results of the domain names that match File 2 from File 1?

Thank you in advance!

like image 249
user1742682 Avatar asked Oct 13 '12 02:10

user1742682


1 Answers

Assuming that order does not matter,

grep -F -f FILE2 FILE1

should do the trick. (This works because of a little-known fact: the -F option to grep doesn't just mean "match this fixed string," it means "match any of these newline-separated fixed strings.")

like image 172
zwol Avatar answered Sep 26 '22 08:09

zwol