通过ip地址连接文件



我有两个文件,它们都有不同列中的ip地址、mac和描述列表。两个文件中都有一些ip地址。我想通过ip地址连接这些文件,以便输出文件具有:1) 文件和2) 两个文件中列出的任何IP后面都有四列2,每个列来自包含mac和描述的每个文件。

文件1:

11.16.31.13     00:a0:c8:b5:c2:d5  keshav-ae1.0
10.16.31.17     f0:ad:4e:01:c5:c8  keshav-ge-2/1/5.0
108.16.31.3     4c:96:14:5d:5f:f0  keshav-ae0.0
108.16.31.4     00:0a:9c:52:74:b2  keshav-ae1.0
27.16.32.1      00:00:5e:00:01:4c  keshav-ae0.0

文件2:

192.16.31.10     00:25:90:cd:4e:3c  keshav-ae0.0
10.16.31.17     f0:ad:4e:01:c5:c8  keshav-ae0.0
17.16.31.2      b0:a8:6e:28:87:f0  keshav-ae0.0
108.16.31.4      00:0a:9c:52:74:b2  keshav-ae0.0
10.16.31.5      2c:36:f8:ce:65:42  keshav-ae0.0

输出文件应为:

11.16.31.13     00:a0:c8:b5:c2:d5  keshav-ae1.0
10.16.31.17     f0:ad:4e:01:c5:c8  keshav-ge-2/1/5.0 f0:ad:4e:01:c5:c8  keshav-ae0.0
108.16.31.3     4c:96:14:5d:5f:f0  keshav-ae0.0
108.16.31.4     00:0a:9c:52:74:b2  keshav-ae1.0 00:0a:9c:52:74:b2  keshav-ae0.0
27.16.32.1      00:00:5e:00:01:4c  keshav-ae0.0
192.16.31.10    00:25:90:cd:4e:3c  keshav-ae0.0
17.16.31.2      b0:a8:6e:28:87:f0  keshav-ae0.0
10.16.31.5      2c:36:f8:ce:65:42  keshav-ae0.0

我尝试过使用排序文件("sort-n"或"sort-n-t.-k 1,1-k 2,2-k 3,3-k 4,4")进行联接,但它没有给出所需的输出。

sort -n file1 > file3
sort -n file2 > file4
join -j 1 -a 1 -a 2 -e UNKNOWN file3 file4 > output

输出文件如下所示:

10.16.31.17 f0:ad:4e:01:c5:c8 keshav-ge-2/1/5.0 f0:ad:4e:01:c5:c8 keshav-ae0.0
10.16.31.5 2c:36:f8:ce:65:42 keshav-ae0.0
11.16.31.13 00:a0:c8:b5:c2:d5 keshav-ae1.0
17.16.31.2 b0:a8:6e:28:87:f0 keshav-ae0.0
108.16.31.4 00:0a:9c:52:74:b2 keshav-ae0.0
192.16.31.10 00:25:90:cd:4e:3c keshav-ae0.0
27.16.32.1 00:00:5e:00:01:4c keshav-ae0.0
108.16.31.3 4c:96:14:5d:5f:f0 keshav-ae0.0
108.16.31.4 00:0a:9c:52:74:b2 keshav-ae1.0

如果您想保持异常输出的顺序,可以尝试一下。

awk 'ARGIND<3{dic[$1]=dic[$1]" "$2" "$3}ARGIND>=3 && !($1 in a){print $1dic[$1];a[$1]}' file1 file2 file1 file2

输出

11.16.31.13 00:a0:c8:b5:c2:d5 keshav-ae1.0
10.16.31.17 f0:ad:4e:01:c5:c8 keshav-ge-2/1/5.0 f0:ad:4e:01:c5:c8 keshav-ae0.0
108.16.31.3 4c:96:14:5d:5f:f0 keshav-ae0.0
108.16.31.4 00:0a:9c:52:74:b2 keshav-ae1.0 00:0a:9c:52:74:b2 keshav-ae0.0
27.16.32.1 00:00:5e:00:01:4c keshav-ae0.0
192.16.31.10 00:25:90:cd:4e:3c keshav-ae0.0
17.16.31.2 b0:a8:6e:28:87:f0 keshav-ae0.0
10.16.31.5 2c:36:f8:ce:65:42 keshav-ae0.0

解释

ARGIND<3意味着下面的语句{dic[$1]=dic[$1]" "$2" "$3}只处理前2个输入文件,而语句{dic[$1]=dic[$1]" "$2" "$3}只是将数据放入字典(在Python中)或映射(在C++/Java等中)dic中,使用ip作为键,使用带描述的mac作为值。

ARGIND>=3表示以下语句{print $1dic[$1];a[$1]}将处理除前两个文件之外的其他文件。!($1 in a)表示字典a中没有的关键字,因此输出不会重复。语句{print $1dic[$1];a[$1]}只打印ipmacdescription,并将ip更新为字典a

这里的第三个和第四个参数只是扫描文件中的键,这样结果就可以按您想要的输出。


如果你只想加入表格,而不在乎输出的顺序,你可以使用

awk '{dic[$1]=dic[$1]" "$2" "$3}END{for(item in dic) print item""dic[item]}' file1 file2

输出

17.16.31.2 b0:a8:6e:28:87:f0 keshav-ae0.0
192.16.31.10 00:25:90:cd:4e:3c keshav-ae0.0
108.16.31.3 4c:96:14:5d:5f:f0 keshav-ae0.0
108.16.31.4 00:0a:9c:52:74:b2 keshav-ae1.0 00:0a:9c:52:74:b2 keshav-ae0.0
10.16.31.5 2c:36:f8:ce:65:42 keshav-ae0.0
27.16.32.1 00:00:5e:00:01:4c keshav-ae0.0
11.16.31.13 00:a0:c8:b5:c2:d5 keshav-ae1.0
10.16.31.17 f0:ad:4e:01:c5:c8 keshav-ge-2/1/5.0 f0:ad:4e:01:c5:c8 keshav-ae0.0

解释语句{dic[$1]=dic[$1]" "$2" "$3}执行相同的操作(将日期放入字典dic中),语句END{for(item in dic) print item""dic[item]}表示在处理完每个输入文件后,对于字典中的每个键item,打印键和值。

awk '!/^$/{ if(!a[$1]){a[$1]=$1"t"$2"t"$3} else {a[$1]=a[$1]"ntt"$2"t"$3} } END { for(i in a) print a[i] }' f1 f2

输出:

17.16.31.2      b0:a8:6e:28:87:f0       keshav-ae0.0
192.16.31.10    00:25:90:cd:4e:3c       keshav-ae0.0
108.16.31.3     4c:96:14:5d:5f:f0       keshav-ae0.0
108.16.31.4     00:0a:9c:52:74:b2       keshav-ae1.0
00:0a:9c:52:74:b2       keshav-ae0.0
10.16.31.5      2c:36:f8:ce:65:42       keshav-ae0.0
27.16.32.1      00:00:5e:00:01:4c       keshav-ae0.0
11.16.31.13     00:a0:c8:b5:c2:d5       keshav-ae1.0
10.16.31.17     f0:ad:4e:01:c5:c8       keshav-ge-2/1/5.0
f0:ad:4e:01:c5:c8       keshav-ae0.0

这个awk应该有帮助:

awk '{a[$1]=a[$1]" "$2" "$3}END{for (i in a){print i a[i]}}' file1 file2

说明:对于每个IP地址,记录mac地址&描述(如果有,请将其附加到现有条目中。)&最后打印所有内容。

如果要按IP地址对数组进行排序,请在for循环之前添加PROCINFO["sorted_in"]="@ind_num_asc"

awk '{a[$1]=a[$1]" "$2" "$3}END{PROCINFO["sorted_in"]="@ind_num_asc"; for (i in a){print i a[i]}}' file1 file2

相关内容

  • 没有找到相关文章

最新更新