The problem is that: I have different txt files in which is registered a timestamp and an ip address for every malware packet that arrives to a server. What I want to do is create another txt file that shows, for every ip, the first time a malware packet arrives.
In general I want to do something like this :
for every line in file.txt
if (ip is not present in list.txt)
copy timestamp and ip in list.txt
I'm using awk for doing it. The main problem is the "if ip is not present in list.txt". I'm doing this:
{ a=$( grep -w "$3" list.txt | wc -c );
if ( a == 0 )
{
#copy timestamp and ip in list.txt
}
( i'm using $3 because the ip address is in the third column of the source file )
I don't know how to make awk evaluate the grep function. I've tried with backticks also but it didn't work. Someone could give me some hint?
I'm testing my script on test file like this:
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
What should I obtain is:
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
Thanks to your help I've succeded in creating the script that fits my needs :
awk '
FILENAME == ARGV[1] {
ip[$2] = 1
next
}
! ($2 in ip) {
print $1, $2 >> ARGV[1]
ip[$2] = 1
}
' list.txt file.txt