用另一个文件中的值replace文件中的string

我有两个文件。 其中之一定义了一组数值对( fileA ):

1 asm 2 assert 3 bio 4 bootasm 5 bootmain 6 buf 7 cat 8 console 9 defs 10 echo 

另一个文件包含一堆值对,如下所示( fileB ):

 bio types bio defs bio param bio spinlock bio buf bootasm asm bootasm memlayout bootasm mmu bootmain types bootmain elf bootmain x86 bootmain memlayout cat types cat stat cat user 

我想编写一个脚本,用文件A中的相应编号replace文件B上的值。不pipe它是生成新文件还是更改现有文件B都无关紧要。

有任何想法吗? 谢谢

 awk 'NR==FNR{a[$2]=$1;next}{$1=a[$1];}1' fileA fileB 

NR == FNR {a [$ 2] = $ 1; next} =>处理fileA时,这是正确的。 一个关联数组形成索引是以第一列作为其值的第二列。

{$ 1 = a [$ 1];} =>处理第二个文件时,将第一列替换为数组中存储的相应值。

1 =>打印每一行。

这可能适用于你(GNU sed):

 sed 's|^\s*\(\S*\)\s*\(.*\)$|/^\2\\>/s//\1/|' fileA | sed -f - fileB