Using awk how do I combine data in two files and substitute values from the second file to the first file?

后端 未结 2 1289
故里飘歌
故里飘歌 2021-01-17 08:56

Any ideas how to the following using awk?

Two input files, data.txt and keys.txt:

data.txt contains some data:

A;1
B;2
A;3

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 09:46

    awk to the rescue!

    assumes the second file has unique keys unlike first file (if not you need to specify what happens then)

    $ awk 'BEGIN   {FS=OFS=";"} 
           NR==FNR {a[$1]=$2; next} 
           $1 in a {print $0,a[$1]}' file2 file1
    
    A;1;30
    B;2;20
    A;3;30
    

    ps. note the order of files...

提交回复
热议问题