只需要 dnsmasq.log 文件中 URL 的父域

Need only parent domain from a URL in dnsmasq.log file

我想从 dnsmasq.log 文件中获取连接的 LAN 客户端访问的网站名称。到目前为止,我已经能够完成这项工作。

cat /tmp/dnsmasq.log | grep query | egrep -v 'AAA|SRV|PTR' |  awk '{print " "" "","","}'

May 29 12:00:17,127.0.0.1,ftp.box.com

May 29 12:00:33,10.0.0.41,2.android.pool.ntp.org

我需要输出为

May 29 12:00:17,127.0.0.1,box.com

May 29 12:00:33,10.0.0.41,ntp.org

我只需要输出中的父域名。请帮忙。

谢谢

您能否尝试按照显示的 samples.Considering 进行编写和测试,您需要 url.

的最后两个元素
awk 'BEGIN{FS=OFS=","} {num=split($NF,array,".");$NF=array[num-1]"."array[num]} 1' Input_file

说明:添加详细说明。

awk '                                ##Starting awk program from here.
BEGIN{                               ##Starting BEGIN section of this awk program from here.
  FS=OFS=","                         ##Setting field separator and output field separator as comma here.
}
{
  num=split($NF,array,".")           ##Splitting last field and metioning . as separator.
  $NF=array[num-1]"."array[num]      ##Setting last column value as 2nd last element DOT and last element of array here.
}
1                                    ##1 will print lines here.
'  Input_file                        ##Mentioning Input_file name here.