带有IP和端口的GREP和AWK文件
我需要一些帮助我有一个文件,其中每个列上都有主机IP和端口,因此该文件如下所示带有IP和端口的GREP和AWK文件,awk,grep,Awk,Grep,我需要一些帮助我有一个文件,其中每个列上都有主机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: 1
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实现这一点的人,也请解释一下语法以便我理解,提前谢谢
我尝试过的
在不同的文件上获取主机和端口,然后使用“粘贴”命令将它们粘贴到新文件中,但问题是,ip在不同的端口上重复,我希望数据保持干净
我在谷歌上搜索并找到了一些这样的命令: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{forx在一个打印中,a[x]''sed''s/',/g'/sed's/,/F''s/,v-k1d/'
但我很想了解它的功能,因为在我的文件中,它没有按预期工作。请尝试以下内容
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.
再来一点
输出:
输入:
可读性更好的版本:
以下带有sed和awk的短管道: 通过以下输入:
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
在上测试。所以我们鼓励用户添加他们为解决自己的问题所付出的努力,所以请添加相同的努力,然后让我们知道。当然,请在您的问题中添加这些细节,然后让我们知道。好的,我们都学习了。但是无论你尝试了什么,请在你的问题中发布它们,而不是在评论中,干杯。我编辑了问题抱歉,我添加了一些我不知道的事情。知道了,但它不起作用,它会打印时间戳。我想也许你有一个带标签的文件,没有空格。是的,文件没有标签spaces@n00b只需交叉检查此处显示的输入文件是否与原始输入匹配,F'[/]'在读取文件时使用空格或正斜杠作为列分隔符,执行awk-F'[/]''{print$4,$6}'infle,如果其打印ip和端口正确,则应能正常工作。
awk -F '[ /]' '{
arr[$4] = $4 in arr ? arr[$4] "," $6 : $6
}
END{
for(i in arr)
print i,arr[i]
}' infile
# 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