Original problem: I have a list of subdomains of one domain, e.g., a.domain.fr
, b.domain.fr
, and so one. The domain itself can be in the list.
I want to find domain.fr
from this list of domain, which is finding the longest common suffix that does not start with a dot .
.
The list of domains is a bash string, and the domains are separated by a single space.
I read Longest common prefix of two strings in bash but I did not manage to convert it to suffix:
echo $domains | tr ' ' '\n' | sed -e 'N;s/^.*\(.*\)\n.*\1$/\1/'
...prints a bunch of empty lines, and:
echo $domains | tr ' ' '\n' | sed -e 'N;s/^.*\.\(.*\)\n.*\.\1$/\1/'
...prints a bunch of fr
.
I am not looking for extreme portability, something that works on any Linux distribution without extra installation is ok for me.
I am looking for a solution that can find sub-domain as the common "domain", e.g., for the following list:
a.d.domain.fr b.d.domain.fr c.d.domain.fr
...the common domain should be d.domain.fr
, but if you have an efficient solution that only works for top-domain (e.g., that would return domain.fr
for the above list), I am also interested.
Sample strings (one sample per line):
a.domain.fr domain.fr b.a.domain.fr b.domain.fr u.domain.fr
domain.fr
a.domain.fr
a.domain.fr b.domain.fr domain.fr
a.d.domain.fr b.d.domain.fr c.d.domain.fr