从另一个文件中的相应条目中减去一个文件中的所有条目,而不考虑缺失值

Subtract all entries in one file from corresponding entries in another file without considering the missing value

我有两个行数和列数相同的文件。我想从另一个文件中的相应条目中减去一个文件中的所有条目,而不考虑缺失值。例如

ifile1.txt    
3  5  2  2
1  ?  2  1
4  6  5  2
5  5  7  1

ifile2.txt
1  2  1  3
1  3  0  2
2  ?  5  1
0  0  1  1

这里是“?”是缺失值,计算时不应考虑。

ofile.txt i.e. [(ifile1.txt) - (ifile2.txt)]
2.00  3.00  1.00 -1.00 
0.00  ?     2.00 -1.00 
2.00  ?     0.00  1.00 
5.00  5.00  6.00  0.00 

我可以通过以下方式在没有任何缺失值的情况下做到这一点。但是不能像这里的“?”这样的缺失值成功。

paste ifile1.txt ifile2.txt > ifile3.txt
awk '{n=NF/2; for (i=1;i<=n;i++) printf "%5.2f ", $i-$(i+n); print ""}' ifile3.txt > ofile.txt

没有测试,但像这样的东西应该可以工作

$ paste ifile1.txt ifile2.txt |
  awk -v q='?' '{n=NF/2; 
                 for(i=1;i<=n;i++) 
                   printf "%5s ", (($i==q||$(i+n)==q)?q:$i-$(i+n)); 
                 print ""}' > ofile.txt

我开始研究如何同时读取 2 个文件,即。如何松开 paste。在 awk 中:

$ cat mm.awk
NR>FNR { exit }                       # after file1 exit
{
    split([=10=],a," ")                   # split record from file1 to array a
    getline < ARGV[2]                 # read record from the file2
    for(i=1;i<=NF;i++)                # one for for both 
        printf "%s%s", ( a[i]!="?" && $i!="?" ? a[i]-$i : "?" ), ( i==NF ? ORS : OFS )
}                                     # on prev rec, if "?" in either, output "?"

运行它:

$ awk mm.awk file1 file2
2 3 1 -1
0 ? 2 -1
2 ? 0 1
5 5 6 0