我有两个文件,一个是带有IP和主机信息的'主列表',另一个是动态填充IP和用户代理字符串。见下面的例子。
24.143.206.32 Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
66.39.66.63 Dalvik/1.6.0 (Linux; U; Android 4.2.1; Nexus 7 Build/JOP40D)
24.143.206.32 # New Host US,city,44.8824996948,-99.6440963745
66.39.66.63 # New Host US,city,44.8824996948,-99.6440963745
and on and on
我需要找到两个AND输出匹配之间的IP匹配,并且尾随信息为BOTH。
24.143.206.32 Browser: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0) LOCATION: New Host US,city,44.8824996948,-99.6440963745
66.39.66.63 Browser: Dalvik/1.6.0 (Linux; U; Android 4.2.1; Nexus 7 Build/JOP40D) LOCATION: New Host US,city,44.8824996948,-99.6440963745
目前我正在使用它进行IP匹配:
awk 'FNR==NR{ a[$1]=$0;next } ($1 in a)' file1 file2 > matchesfile
我一遍又一遍地试过,却无法得到我需要的所有信息。你能提供任何帮助还是太复杂了?我不确定这个方向。
答案 0 :(得分:1)
我就是这样做的:
% awk ' {
IP = $1
$1 = ""
}
FNR == NR {
browser[IP] = $0
}
FNR != NR && IP in browser {
printf "%s Browser:%s Location:%s\n", IP, browser[IP], $0
}
' file[12]
24.143.206.32 Browser: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0) Location: # New Host US,city,44.8824996948,-99.6440963745
66.39.66.63 Browser: Dalvik/1.6.0 (Linux; U; Android 4.2.1; Nexus 7 Build/JOP40D) Location: # New Host US,city,44.8824996948,-99.6440963745
答案 1 :(得分:1)
如果你想要两个输出的数据,那么你需要打印信息,测试的方式略有不同,我想:
awk 'FNR == NR { a[$1] = $0; next }
{ if ($1 in a) print $0 " " a[$1] }' file1 file2 > matchesfile
这与你所拥有的非常接近;但是,印刷是不同的。您隐式调用了print $0
。至少使用GNU awk
,您可以使用条件作为模式:
awk 'FNR == NR { a[$1] = $0; next }
($1 in a) { print $0 " " a[$1] }' file1 file2 > matchesfile
如果你想在输出中加入'Browser:'和'Location:'标签,那么它需要更多的工作:
awk 'FNR == NR { for (i = 2; i < NF; i++) a[$1] = a[$i] " " $i; next }
($1 in a) { for (i = 2; i < NF; i++) loc = loc " " $i;
print $1 " Browser: " a[$1] " Location: " loc }
' file1 file2 > matchesfile
第一个for
循环将IP地址后的file1
的浏览器字段连接到a[$1]
。
对于从for
到变量file2
的位置信息,第二个loc
循环执行相同的操作。
print
然后吐出数据。您可以根据自己的要求微调格式。
还有其他方法可以达到相同的效果......
答案 2 :(得分:1)
awk '
FNR==NR{ a[$1]=$0; next }
$1 in a {
sub(/[[:space:]]+/,"&Browser: ",a[$1])
sub(/[^[:space:]]+[[:space:]]+#/,"LOCATION:")
print a[$1], $0
}
' file1 file2 > matchesfile
答案 3 :(得分:0)
awk 'FNR==NR{f=$1;$1=$2="";a[f]=$0;next}($1 in a ){$2="Browser: "$2;print $0,a[$1]}' file2 file1