簡體   English   中英

AWK 有條件地從文件中選擇 FQDN 主機名

[英]AWK to pick FQDN hostname conditionally from the File

專家們在閱讀了如何提供最小可重現示例后我又來了,我再次提出問題。

我想過濾完全限定的主機名(例如: dtc4028.ptc.db01.delta.com )並計算單個主機上的重復。

以下是我的原始數據:

Feb 24 07:20:56 dbv0102 postfix/smtpd[29531]: NOQUEUE: reject: RCPT from dtc4023.ptc.db01.delta.com[172.10.10.161]: 554 5.7.1 <beta_st@dtc.com>: Sender address rejected: Access denied; from=<beta_st@dtc.com> to=<stordb@dtc.com> proto=ESMTP helo=<dtc4023.ptc.db01.delta.com>
Feb 24 07:21:20 dbv0102 postfix/smtpd[29528]: NOQUEUE: reject: RCPT from dtc4023.ptc.db01.delta.com[172.10.10.161]: 554 5.7.1 <beta_st@dtc.com>: Sender address rejected: Access denied; from=<beta_st@dtc.com> to=<stordb@dtc.com> proto=ESMTP helo=<dtc4023.ptc.db01.delta.com>
Feb 21 05:05:06 dbv0102 postfix/smtpd[32001]: disconnect from dtc4028.ptc.db01.delta.com[172.12.78.81]
Feb 21 05:05:23 dbv0102 postfix/smtpd[32010]: connect from dtc4028.ptc.db01.delta.com[172.12.78.81]
Feb 21 05:06:15 dbv0102 postfix/smtpd[31994]: connect from dtc3024.ptc.db01.delta.com[172.10.10.166]
Feb 21 05:06:15 dbv0102 postfix/smtpd[31994]: disconnect from dtc3024.ptc.db01.delta.com[172.10.10.166]
Feb 21 13:05:08 dbv0102 postfix/smtpd[29043]: lost connection after CONNECT from dtc4028.ptc.db01.delta.com[172.12.78.81]
Feb 21 13:05:08 dbv0102 postfix/smtpd[29048]: lost connection after CONNECT from dtc4028.ptc.db01.delta.com[172.12.78.82]

我自己嘗試了什么:

我在這里做什么,只取所需的1,2,4 and 8

$ awk '/from dtc/{print $1, $2, $4, $8}' maillog.log
Feb 24 dbv0102 RCPT
Feb 24 dbv0102 RCPT
Feb 21 dbv0102 dtc4028.ptc.db01.delta.com[172.12.78.81]
Feb 21 dbv0102 dtc4028.ptc.db01.delta.com[172.12.78.81]
Feb 21 dbv0102 dtc3024.ptc.db01.delta.com[172.10.10.166]
Feb 21 dbv0102 dtc3024.ptc.db01.delta.com[172.10.10.166]
Feb 21 dbv0102 after
Feb 21 dbv0102 after

其次,我刪除RCPT|after因為這些行沒有主機名,然后也刪除[]以僅擁有主機名並計算它們的重復。

$ awk '/from dtc/{print $1, $2, $4, $8}' maillog.log| egrep -v "RCPT|after" | awk '{print $4}'| cut -d"[" -f1 | uniq -c
      2 dtc4028.ptc.db01.delta.com
      2 dtc3024.ptc.db01.delta.com

我的願望:

我希望這可以用 awk 本身更智能地編寫,而不是我這樣做很臟。

注意:我們能否僅在第 6 列之后獲取 FQDN 主機名,例如dtc4028.ptc.db01.delta.com

根據您顯示的示例,您能否嘗試以下操作。 在 GNU awk中編寫和測試。

awk '
match($0,/from .*com\[/){
  count[substr($0,RSTART+5,RLENGTH-6)]++
}
END{
  for(key in count){
    print count[key],key
  }
}
' Input_file

說明:為上述添加詳細說明。

awk '                                      ##Starting awk program from here.
match($0,/from .*com\[/){                  ##Using match function to match regex from .*com\[
  count[substr($0,RSTART+5,RLENGTH-6)]++   ##Whenever match is having a regex matched so it sets RSTART and RLENGTH, RSTART tells us starting point of matched regex and RLENGTH is complete length.
}
END{                                       ##Starting END block of this program from here.
  for(key in count){                       ##Traversing through count array here.
    print count[key],key                   ##Printing its key and value here.
  }
}
' Input_file                               ##Mentioning Input_file name here.
$ awk -F'[[ ]' '$8=="from"{ cnt[$9]++ } END{ for (host in cnt) print cnt[host], host }' file
2 dtc4028.ptc.db01.delta.com
2 dtc3024.ptc.db01.delta.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM