带有 IP 和端口的 GREP 和 awk 文件



我需要一些帮助,我有一个文件,其列上有主机 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

但我很想了解它的作用,因为在我的文件中它没有按预期工作。

你能试试下面的吗?

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

在重复上进行了测试。

最新更新