AWK 关联数组、映射或哈希映射

cxs*_*tam 3 mapping awk associative-array

假设我有两个文件:

文件 1 - map.txt

1, 178246
2, 289789
3, 384275
4, 869282
Run Code Online (Sandbox Code Playgroud)

文件 2 - 关系.txt

178246, 289789
384275, 178246
384275, 869282
Run Code Online (Sandbox Code Playgroud)

预期结果是:

1, 2
3, 1
3, 4
Run Code Online (Sandbox Code Playgroud)

但是我使用以下代码得到的结果是:

awk 'FNR==NR{map[$2]=$1} {$1=map[$1];$2=map[$2];print $0}' map.txt relation.txt

  2,
  1,
  4,
Run Code Online (Sandbox Code Playgroud)

当我像这样交换 map.txt 中的列时,它很困惑:

178246, 1
289789, 2
384275, 3
869282, 4
Run Code Online (Sandbox Code Playgroud)

relationship.txt 不会改变

结果变成了:

awk 'FNR==NR{map[$1]=$2} {$1=map[$1];$2=map[$2];print $0}' map.txt relation.txt

1,
3,
3,
Run Code Online (Sandbox Code Playgroud)

{$1=map[$1];$2=map[$2];print $0} 附近似乎有问题

End*_*oro 5

awk  -F"[, ]" 'NR==FNR {m[$3]=$1;next};{print m[$1]",",m[$3]}' map.txt relations.txt
Run Code Online (Sandbox Code Playgroud)