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

Shell 只需要dnsmasq.log文件中URL的父域,shell,awk,dnsmasq,Shell,Awk,Dnsmasq,我想从dnsmasq.log文件中获取连接的LAN客户端访问的网站名称。到目前为止,我已经能够做到这一点 cat /tmp/dnsmasq.log | grep query | egrep -v 'AAA|SRV|PTR' | awk '{print $1" "$2" "$3","$8","$6}' 5月29日12:00:17127.0.0.1,ftp.box.com 5月29日12:00:33,10.0.0.41,2.android.pool.ntp.org 我需要输出为 5月29日12:

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

cat /tmp/dnsmasq.log | grep query | egrep -v 'AAA|SRV|PTR' |  awk '{print $1" "$2" "$3","$8","$6}'
5月29日12:00:17127.0.0.1,ftp.box.com

5月29日12:00:33,10.0.0.41,2.android.pool.ntp.org

我需要输出为

5月29日12:00:17127.0.0.1,box.com

5月29日12:00:33,10.0.0.41,ntp.org

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


谢谢

考虑到您需要url的最后2个元素,请尝试以下内容,并用显示的示例编写和测试

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.

考虑到您需要url的最后2个元素,请尝试以下内容,并用显示的示例编写和测试

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.

感谢@RavinderSingh13的快速响应。命令未给出所需的输出,root@OpenWrt:~/scripts#grep“query”/tmp/dnsmasq.log | egrep-v'AAA | SRV | PTR'| awk'BEGIN{FS=OFS=“,”}{num=split($6,array),“.”;$6=array[num-1]”array[num];print$1“$2”$3,$8,$6}5月29日13:15:02 dnsmasq[25328]:query[A]www.google.com,127.0.0.1,,。5月29日13:15:43 dnsmasq[25328]:从10.0.0.2,,,查询[A]presence.teams.microsoft.com,,。5月29日13:15:51 dnsmasq[25328]:从10.0.0.35开始查询[A]hclo365-my.sharepoint.com,@nbuser,你真的应该发布
/tmp/dnsmasq.log
的示例,因为你想用你的命令加入这个示例,然后尝试类似于
cat/tmp/dnsmasq.log | grep query | egrp-v'AAA | SRV | PTR | awk{print$1“$2$3”、$8”、“$6}“|awk”BEGIN{FS=OFS=“,”}{num=split($NF,array,“.”;$NF=array[num-1]”“array[num]}.1'
但请相信我,这根本没有效率,您可以测试它,但更好的是,您可以让您的样本知道问题中的所有细节,以便在一个
awk
中完成它,这是单手操作的。@nbuser,在你的问题中,请不要在评论中。谢谢这个命令起作用了。cat/tmp/dnsmasq.log | grep query | egrep-v'AAA | SRV | PTR'| awk'{print$1'$2'$3',“$8”,“$6}'| awk'BEGIN{FS=OFS=”,“}{num split($NF,array,“.”;$NF array=array[num 1]”我确信一定有更好的方法来处理它,但现在它解决了我的问题。再次感谢您的帮助,并将您的答案标记为解决方案。感谢@RavinderSingh13的快速响应。命令未给出所需的输出,root@OpenWrt:~/scripts#grep“query”/tmp/dnsmasq.log | egrep-v'AAA | SRV | PTR'| awk'BEGIN{FS=OFS=“,”}{num=split($6,array),“.”;$6=array[num-1]”array[num];print$1“$2”$3,$8,$6}5月29日13:15:02 dnsmasq[25328]:query[A]www.google.com,127.0.0.1,,。5月29日13:15:43 dnsmasq[25328]:从10.0.0.2,,,查询[A]presence.teams.microsoft.com,,。5月29日13:15:51 dnsmasq[25328]:从10.0.0.35开始查询[A]hclo365-my.sharepoint.com,@nbuser,你真的应该发布
/tmp/dnsmasq.log
的示例,因为你想用你的命令加入这个示例,然后尝试类似于
cat/tmp/dnsmasq.log | grep query | egrp-v'AAA | SRV | PTR | awk{print$1“$2$3”、$8”、“$6}“|awk”BEGIN{FS=OFS=“,”}{num=split($NF,array,“.”;$NF=array[num-1]”“array[num]}.1'
但请相信我,这根本没有效率,您可以测试它,但更好的是,您可以让您的样本知道问题中的所有细节,以便在一个
awk
中完成它,这是单手操作的。@nbuser,在你的问题中,请不要在评论中。谢谢这个命令起作用了。cat/tmp/dnsmasq.log | grep query | egrep-v'AAA | SRV | PTR'| awk'{print$1'$2'$3',“$8”,“$6}'| awk'BEGIN{FS=OFS=”,“}{num split($NF,array,“.”;$NF array=array[num 1]”我确信一定有更好的方法来处理它,但现在它解决了我的问题。再次感谢您的帮助,并将您的答案标记为解决方案。