桑达,awk中,grep或者别的东西。从名单中删除,如果子域父域present [英] Sed, awk, grep or something else. Remove subdomains from list if parent domain present

查看:94
本文介绍了桑达,awk中,grep或者别的东西。从名单中删除,如果子域父域present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有域的列表:

test.example.com
example.com
example.test.com
test.test.com
test.com
test.example.example.org
example.example.org

我需要删除,如果父域present所有子域。
输出必须是这样的:

I need to remove all subdomains if parent domain present. Output will must be like this:

example.com
test.com
example.example.org

这是可能的吗?

推荐答案

下面取决于这不是POSIX标准,但更是一应俱全。

The following depends on rev which is not Posix standard but is readily available.

rev file.txt |
sort |
awk 'NR!=1&&substr($0,0,length(p))==p{next}{p=$0".";print}' |
rev

男人转

转速命令是UTIL-linux软件包的一部分,可从 ftp://ftp.kernel.org/pub/linux/utils/util-linux/

The rev command is part of the util-linux package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/

可以实现与 AWK 反向(它没有被逐个字符;这一个段云段):

You can implement reverse with awk (it doesn't have to be character by character; this one goes segment by segment):

awk -F. '{for (i=NF; i>1; --i) printf "%s.",$i;print $1}'

使用,上面的管道变得有点长:

Using that, the above pipeline becomes a bit longer:

awk -F. '{for (i=NF; i>1; --i) printf "%s.",$i;print $1}' file.txt |
sort |
awk -F. 'NR!=1&&substr($0,0,length(p))==p{next}
         {p=$0".";for (i=NF; i>1; --i) printf "%s.",$i;print $1}'

这篇关于桑达,awk中,grep或者别的东西。从名单中删除,如果子域父域present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆