Extract domain names from a file in Shell [closed]

时光毁灭记忆、已成空白 提交于 2019-11-29 17:44:36

You can use grep:

grep -Eo '[^.]+\.[^.]+$' file.txt

Example:

$ cat file.txt
sub1.domain.com
sub2.domains2.com
domain3.com
sub5.domain.ext
subof.subdomain.domainx.ex2

$ grep -Eo '[^.]+\.[^.]+$' file.txt
domain.com
domains2.com
domain3.com
domain.ext
domainx.ex2

Note that this will return co.uk for www.google.co.uk.

ikegami

A possible solution using Perl:

use Domain::PublicSuffix qw( );

my $dps = Domain::PublicSuffix->new();

for my $host (qw(
   www.google.com
   foo.bar.google.com
   www.google.co.uk
   foo.bar.google.co.uk
)) {
   my $root = $dps->get_root_domain($host)
      or die $dps->error();

   say $root;
}

Output:

google.com
google.com
google.co.uk
google.co.uk
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!