问题是:我有不同的 txt 文件,其中为到达服务器的每个恶意软件数据包注册了时间戳和 IP 地址。我想要做的是创建另一个 txt 文件,该文件显示,对于每个 ip,恶意软件数据包第一次到达。
一般来说,我想做这样的事情:
for every line in file.txt
if (ip is not present in list.txt)
copy timestamp and ip in list.txt
我正在使用 awk 来做这件事。主要问题是“如果 ip 不在 list.txt 中”。我正在这样做:
{ a=$( grep -w "$3" list.txt | wc -c );
if ( a == 0 )
{
#copy timestamp and ip in list.txt
}
(我使用 $3 因为 IP 地址在源文件的第三列)
我不知道如何让 awk 评估 grep 函数。我也尝试过使用反引号,但它没有用。有人可以给我一些提示吗?
我正在像这样的测试文件上测试我的脚本:
10 192.168.1.1
11 192.168.1.2
12 192.165.2.4
13 122.11.22.11
13 192.168.1.1
13 192.168.1.2
13 122.11.22.11
14 122.11.22.11
15 122.11.22.11
15 122.11.22.144
15 122.11.2.11
15 122.11.22.111
我应该得到的是:
10 192.168.1.1
11 192.168.1.2
12 192.165.2.4
13 122.11.22.11
15 122.11.22.144
15 122.11.2.11
15 122.11.22.111
感谢您的帮助,我成功地创建了适合我需要的脚本:
awk '
FILENAME == ARGV[1] {
ip[$2] = 1
next
}
! ($2 in ip) {
print $1, $2 >> ARGV[1]
ip[$2] = 1
}
' list.txt file.txt