我需要将文件2的“值”列合并到文件1,使col1-col2唯一对组合保持完整。可以跳过不匹配的组合。我是python / awk的新手。所以,我需要指导方针或想法来解决问题。
我没有权限使用python pandas包,所以会欣赏其他替代解决方案。
在此先感谢您的支持。
col1 col2
559 1 558 1
559 0 558 0
558 1 557 1
558 0 557 0
504 2 255 0
504 1 192 0
504 0 367 0
255 0 187 1
227 0 185 0
192 0 187 0
187 0 185 1
185 0 183 1
183 0 171 1
171 0 9 0
1236 1 766 0
1236 0 903 0
col1 col2 value
559 1 91987224 2400000000
559 0 91987224 100000000
558 1 64064811 2400000000
558 0 91987224 100000000
557 1 59966218 2400000000
557 0 64064811 100000000
555 0 59966218 2500000000
533 0 87947477 5000000000
504 2 53048833 1000000000
504 1 19225707 100000000
504 0 35811624 5000000000
255 0 1687416 2800000000
227 0 101635574 100000000
192 0 7894664 100000000
187 0 1687416 2900000000
185 0 1687416 3000000000
183 0 1687416 4000000000
171 0 1687416 5000000000
559 1 2400000000
559 0 100000000
558 1 2400000000
558 0 100000000
557 1 2400000000
557 0 100000000
555 0 2500000000
533 0 5000000000
504 2 1000000000
504 1 100000000
504 0 5000000000
255 0 2800000000
227 0 100000000
192 0 100000000
187 0 2900000000
185 0 3000000000
183 0 4000000000
171 0 5000000000
1236 1 -
1236 0 -
$ cat a.awk
# Create an index from the first two columns
{ k = $1 FS $2 }
# Read file2 values (first on the command line) into an array
NR == FNR { a[k] = $4; next }
# Read file1, retrieving and printing the values from file2 where available
# After printing, delete the element we've already used
{ print $1, $2, k in a ? a[k] : "-"; delete a[k] }
# Print any left over elements with their values
END { for (i in a) { print i, a[i] } }
# Run the command noting importance of file order
# Pipe output into reverse sort to match desired ouptut
$ awk -f a.awk file2 file1 | sort -r
559 1 2400000000
559 0 100000000
558 1 2400000000
558 0 100000000
557 1 2400000000
557 0 100000000
555 0 2500000000
533 0 5000000000
504 2 1000000000
504 1 100000000
504 0 5000000000
255 0 2800000000
227 0 100000000
192 0 100000000
187 0 2900000000
185 0 3000000000
183 0 4000000000
171 0 5000000000
1236 1 -
1236 0 -