Linux: Merge files with the same column characters in different files (awk function)

There is file1.txt with the following contents:

H aa 0 0 1 -9

H bb 0 0 2 -9

H cc 0 0 2 -9

There is file2.txt with the following contents:

H aa 0 0 0 -9 asd qwe

H bb  0 0 0 -9 fgh rty

H cc  0 0 0 -9 jkl uio

Now I want to merge files according to the same column characters of file1 and file2. The requirements are as follows: 1. If the second column of the two files is the same, then modify the third, fourth, fifth and sixth columns of file2 to file1 Corresponding contents of the third, fourth, fifth and sixth columns; 2. The seventh and eighth columns of the new file are the seventh and eighth columns of file2.

The content of the merged file file3.txt is as follows:

aa 0 0 1 -9 asd qwe

bb  0 0 2 -9 fgh rty

cc  0 0 2 -9 jkl uio

The command used is:

awk 'NR==FNR{a[$2]=$0;next}NR>FNR{if($2 in a)print a[$1],a[$2],$7,$8}' file1.txt file2.txt > file3.txt

  

Youdao Dictionary
H aa 0 0 0 -9 a ...
detailed X
  H aa 0 0 0 9 asd qwe H bb 0 0 0 9 fgh without H cc 0 0 0 9 . jkl uio

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325345280&siteId=291194637