2012-10-26 52 views
0

我有两个文件。他们中的一个定义了一组数 - 值对如下(的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中的相应编号替换文件B上的值。 生成新文件或更改现有文件B无关紧要。

任何想法? 由于

回答

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

NR == FNR {A [$ 2] = $ 1;下一个} =>当处理的fileA这是真实的。一个关联数组形成索引是以第一列作为其值的第二列。

{$ 1 = [$ 1];} =>当处理所述第二文件时,替换存储在数组中的相应值的第一列中。

1 =>打印每行。

1

这可能会为你工作(GNU SED):

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