replace string in a file with value from another file

前端 未结 2 1504
庸人自扰
庸人自扰 2020-12-18 16:37

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         


        
相关标签:
2条回答
  • 2020-12-18 16:49

    This might work for you (GNU sed):

    sed 's|^\s*\(\S*\)\s*\(.*\)$|/^\2\\>/s//\1/|' fileA | sed -f - fileB
    
    0 讨论(0)
  • 2020-12-18 16:51
    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.

    0 讨论(0)
提交回复
热议问题