簡體   English   中英

帶有 IP 和端口的 GREP 和 AWK 文件

[英]GREP AND AWK file with IP and Ports

我需要一些幫助 我有一個文件,每個文件的列上都有主機 IP 和端口,所以文件看起來像這樣

Timestamp: 1573678793 Host: 192.168.0.1 Ports: 80/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.1 Ports: 443/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.2 Ports: 80/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.2 Ports: 443/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.3 Ports: 8080/open/tcp/

所以我想以這種格式grep主機和端口:

192.168.0.1  80,443
192.168.0.2  80,443
192.168.0.3  8080

任何知道如何使用 awk 和 grep 實現這一點的人,也請解釋語法讓我理解,在此先感謝。

我嘗試過的;

  1. 獲取不同文件上的主機和端口,然后使用 paste 命令將它們粘貼到一個新文件中,但問題是 ip 與不同的端口重復,我希望使數據干凈。

  2. 我用谷歌搜索並找到了一些這樣做的命令: cat ips-ports | | grep Host | awk '{print $2,$7}' | sed 's@/.*@@' | sort -t' ' -n -k2 | awk -F' ' -v OFS=' ' '{x=$1;$1="";a[x]=a[x]","$0}END{for(x in a) print x,a[x]}' | sed 's/, /,/g' | sed 's/ ,/ /' | sort -V -k1 | cut -d " " -f2 cat ips-ports | | grep Host | awk '{print $2,$7}' | sed 's@/.*@@' | sort -t' ' -n -k2 | awk -F' ' -v OFS=' ' '{x=$1;$1="";a[x]=a[x]","$0}END{for(x in a) print x,a[x]}' | sed 's/, /,/g' | sed 's/ ,/ /' | sort -V -k1 | cut -d " " -f2

但我很想了解它的作用,因為在我的文件中它沒有按預期工作。

你能不能試試以下。

awk '
BEGIN{
  OFS=","
}
{
  match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/)
  split($NF,array,"/")
  val=substr($0,RSTART,RLENGTH)
  a[val]=(a[val]?a[val] OFS:"")array[1]
}
END{
  for(i in a){
    print i FS a[i]
  }
}
' Input_file

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

awk '                                              ##Starting awk program from here.
BEGIN{                                             ##Starting BEGIN section from here.
  OFS=","                                          ##Set OFS as comma here.
}                                                  ##Closing BLOCK for BEGIN section here.
{
  match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/)       ##Using match function ti match IP regex here.
  split($NF,array,"/")                             ##Splitting last field into an array named array with delimiter /
  val=substr($0,RSTART,RLENGTH)                    ##Creating a variable named val whose value is sub-string of line with starting point RSTART to RLENGTH.
  a[val]=(a[val]?a[val] OFS:"")array[1]            ##Creating an array named a with index val and concatenate it with its own values.
}
END{                                               ##Starting END BLOCK for this awk program.
  for(i in a){                                     ##Starting for loop here.
    print i FS a[i]                                ##Printing variable i, FS and value of array a with index i here.
  }                                                ##Closing BLOCK for, for loop here.
}                                                  ##Closing BLOCK for END section of this program here.
'  Input_file                                      ##Mentioning Input_file here.

還有一些awk

輸出:

$ awk -F '[ /]' '{arr[$4]=$4 in arr?arr[$4]","$6:$6}END{for(i in arr)print i,arr[i]}' infile
192.168.0.1 80,443
192.168.0.2 80,443
192.168.0.3 8080

輸入:

$ cat infile
Timestamp: 1573678793 Host: 192.168.0.1 Ports: 80/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.1 Ports: 443/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.2 Ports: 80/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.2 Ports: 443/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.3 Ports: 8080/open/tcp/

更好的可讀版本:

awk -F '[ /]' '{
                arr[$4] = $4 in arr ? arr[$4] "," $6 : $6
              }
           END{
                for(i in arr)
                   print i,arr[i]
              }' infile

跟隨sedawk短管道:

# first `sed` with a regex extract the host and ports:
sed 's/.*Host:[[:blank:]]*\([^[:blank:]]*\)[[:blank:]]*Ports:[[:blank:]]*\([0-9]*\)\/.*/\1 \2/' |
# then awk to join the fields with a comma:
awk '{ a[$1] = a[$1] (a[$1]?",":"") $2 }  END{ for (i in a) print i, a[i] }'

具有以下輸入:

Timestamp: 1573678793 Host: 192.168.0.1 Ports: 80/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.1 Ports: 443/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.2 Ports: 80/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.2 Ports: 443/open/tcp/
Timestamp: 1574833457 Host: 192.168.0.3 Ports: 8080/open/tcp/

輸出:

192.168.0.1 80,443
192.168.0.2 80,443
192.168.0.3 8080

repl 上測試。

暫無
暫無

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

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