Home > database >  How to match substring from one file to another file using grep?
How to match substring from one file to another file using grep?

Time:06-17

I have two files. File1 with bunch of email address. File2 with list of domains.

I want to find all the email address matching the domains (also the non-matching ones)

If some one please let me know how can we do this using 'grep' from terminal.

File1.csv
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

File2
hotmail.com
live.com
fb.com

The output should be (and non-matching as well)
[email protected]
[email protected]
[email protected]

Please consider the email file is too big and contains 2M emails to compare against 6k domains.

CodePudding user response:

You can use -f to read the patterns from a file:

grep -f File2 File1.csv

CodePudding user response:

In your comment, you are trying to match a fixed pattern of: @[anydomainNameInFile2.txt]

in this case, you might need to add @ at the beginning of each line in File2 so you can use it as a fixed pattern.

you can do it by the following:

  1. add @ at the beginning of each line in file2.txt using sed command.

    $ sed 's/^/@/' file2.txt  > new-file.txt
    

don't worry that won't miss with your main file, you are saying it's about 2M field, we are saving the output to another file named new-file.txt

  1. run the -f option grep command using the new-file.txt file as the following:

    $ grep -f newfile.txt File1.csv
    
  • Related