awk 'FNR==NR{a[$1,$2]=$3;next}FNR==1{print;next}{print $1,$2,$3/a[$1,$2]}' f1 f2
Experiment Replica Mean
General0 0 0.3052
General0 1 0.307955
General0 2 0.339084
General0 3 0.31702
General0 4 0.320473
提示:column -t
是将输出格式化为表格的好工具:
awk .... | column -t
Experiment Replica Mean
General0 0 0.3052
General0 1 0.307955
General0 2 0.339084
General0 3 0.31702
General0 4 0.320473
解释:
FNR==NR { # FNR==NR is only true in the first file
a[$1,$2]=$3 # Build array, keys are field 1 and 2, value is field 3
next # Skip to the next line in the file
}
FNR==1 { # If we are on the first line in the second file
print # Print the line
next # Go grab the next line
}
{
x=$3/a[$1,$2] # Do the math (the value of a[$1,$2] is $3 from file1)
print $1,$2,x # print the output
}
要以这种形式运行脚本,请将其保存到文件script.awk
并执行awk -f script.awk f1 f2
.