一旦确定了字段数,我就可以用if语句写出来,但我相信AWK的强大功能可以简化这一点。我希望if语句和下面的输出中的逻辑清晰。
确定第一个节点($ 2)到file1中第二个节点(3)的链接号
if($2 && $3 (file1) == $1 and $3 (file2));
print $2 of file2 in between $2 and $3 of file1
elif($2 && $3 (file1) == $3 && $1 (file2));
print $4 of file2 in between $2 and $3 of file1
else
print no_int in between $2 and $3 of file1
如果适用,确定第二个节点($ 3)到file1中第三个节点(4)的链接号
if($3 && $4 (file1) == $1 and $3 (file2));
print $2 of file2 in between $2 and $3 of file1
elif($3 && $4 (file1) == $3 && $1 (file2));
print $4 of file2 in between $3 and $4 of file1
else
print no_int in between $3 and $4 of file1
如果适用,确定第三个节点($ 4)到file1中第四个节点(5)的链接号
if($4 && $5 (file1) == $1 and $3 (file2));
print $2 of file2 in between $4 and $5 of file1
elif($4 && $5 (file1) == $3 && $1 (file2));
print $4 of file2 in between $4 and $5 of file1
else
print no_int in between $4 and $5 of file1
......等等......
文件1:路径表
N1-N4 NODE3 NODE4
N1-N5 NODE2 NODE4 NODE5
N1-N6 NODE3 NODE4 NODE5 NODE6
文件2:链接表
NODE1 1 NODE2 2
NODE1 3 NODE3 4
NODE2 5 NODE4 6
NODE4 8 NODE3 7
NODE4 9 NODE5 10
NODE5 11 NODE6 12
输出:
N1-N4 NODE3 7 NODE4
N1-N5 NODE2 5 NODE4 9 NODE5
N1-N6 NODE3 7 NODE4 9 NODE5 11 NODE6
网络地图
NODE1--1--2--NODE2
| |
3 5
| |
4 6
| |
NODE3--7--8--NODE4--9--10--NODE5--11--12--NODE6
编辑匹配逻辑 例如,来自file1:
的一行N1-N4 NODE3 NODE4
这应匹配文件二中的这一行:
NODE4 8 NODE3 7
$ 2&& $ 3(file1)与$ 1,$ 3(file2)不匹配 - NODE3 NODE4!= NODE4 NODE3。但2美元,3美元等于3美元,1美元 - NODE3 NODE4 == NODE3 NODE4。所以从file2打印$ 4 - “NODE3 7 NODE4”。如果$ 2&& $ 3(file1)DID匹配$ 1,$ 3(file2),从file2打印$ 2 - “NODE3 8 NODE4”
答案 0 :(得分:1)
这样的事情应该这样做,设置样本输入/输出以包含您感兴趣的边缘情况非常重要。
$ awk 'NR==FNR{a[$1,$3]=$2; a[$3,$1]=$4; next}
{for(i=2;i<NF;i++)
{k=a[$i,$(i+1)];
$i=$i OFS (k?k:"NaN")}}1' link path
N1-N4 NODE3 7 NODE4
N1-N5 NODE2 5 NODE4 9 NODE5
N1-N6 NODE3 7 NODE4 9 NODE5 11 NODE6