与值从另一个文件替换字符串的文件 [英] replace string in a file with value from another file
问题描述
我有两个文件。其中一个定义了一组数字值对如下(的fileA )
1 ASM
2断言
3生物
4 bootasm
5 bootmain
6 BUF
7猫
8控制台
9 DEFS
10回声
另一个文件包含了一堆价值配对,如下所示( FILEB )
生物类型
生物DEFS
生物参数
生物自旋锁
生物BUF
bootasm ASM
bootasm memlayout
bootasm MMU
bootmain类型
bootmain小精灵
bootmain 86
bootmain memlayout
猫类型
猫统计
猫用户
我想编写一个脚本,及其对应的编号从文件A.将替换文件B的值
如果它生成一个新文件或改变现有文件B不要紧。
任何想法?
谢谢
的awk'NR == FNR {a [$ 2] = $ 1;接下来} {$ 1 = [$ 1]; } 1'的fileA FILEB
NR == FNR {一个[$ 2] = $ 1;下一个} =>处理时的fileA这是真实的。形成有关联数组,其中索引是与第1列作为其值的第2列。
{$ 1 = [$ 1];}。=>当处理第二文件,具有存储在数组中的相应的值替换1列
1 =>打印每行。
I have two files. One of them defines a set of number-value pairs as follows (fileA):
1 asm
2 assert
3 bio
4 bootasm
5 bootmain
6 buf
7 cat
8 console
9 defs
10 echo
The other file contains a bunch of value pairings, as follows (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
I want to write a script that replaces the values on file B with their corresponding numbers from file A. It does not matter if it generates a new file or changes the existing file B.
Any ideas? Thanks
awk 'NR==FNR{a[$2]=$1;next}{$1=a[$1];}1' fileA fileB
NR==FNR{a[$2]=$1;next} => This is true when the fileA is processed. An associative array is formed where the index is the 2nd column with the 1st column as its value.
{$1=a[$1];} => When the second file is processed, Replace the 1st column with the appropriate value stored in the array.
1 => Print every line.
这篇关于与值从另一个文件替换字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!