DomainsFeatured DomainsFeatured - 1 month ago 8
Linux Question

How To Grep 2 Files With Regex To Find Match At End Of Line

I have the following two files:

full-domains.txt
contains:

example1.com
example2.net
example3.org
example4.info
example5.co


extensions.txt
contains:

net
org
co


I would like to loop the extensions into a grep command that will match only lines that end with the pattern in the extension file.

I'm thinking it's something like:
grep -Ff extensions.txt full-domains.txt
but I'm not sure where to add the syntax to make it match at the end of line with
$
.

The output should be:

example2.net
example3.org
example5.co


Any positive input is highly appreciated. I should add that I'm open to other solutions as well besides
grep
.

Answer

This is clearly a job for awk:

$ awk -F'.' 'NR==FNR{exts[$0]; next} $NF in exts' extensions.txt full-domains.txt
example2.net
example3.org
example5.co