【问题标题】:Print a selective column after subtraction from another file从另一个文件中减去后打印选择列
【发布时间】:2016-09-28 21:46:36
【问题描述】:

我有两个行数和列数相等的文件。我想从另一个文件的第二列中减去一个文件中的第二列,而不考虑缺失值。例如

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. [$2(ifile1.txt) - $2(ifile2.txt)]
3
?
?
5

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

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 > ifile4.txt
awk '{printf ("%.2f\n",$2)}' ifile4.txt > ofile.txt

【问题讨论】:

    标签: linux shell awk


    【解决方案1】:

    POSIX shell 脚本,然后粘贴

    paste ifile[12].txt | \
     while read a b c d  e f g ; do \
           [ "$b$f" -eq "$b$f" ] 2> /dev/null \
             && echo $(( b - f )) \
             || echo '?' ; \
     done
    

    输出:

    3
    ?
    ?
    5
    

    【讨论】:

      【解决方案2】:
      $ awk 'NR==FNR{a[NR]=$2;next} {print ((a[FNR]$2)~/?/ ? "?" : a[FNR]-$2)}' file1 file2
      3
      ?
      ?
      5
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-25
        • 2015-07-06
        • 1970-01-01
        • 2019-07-26
        • 1970-01-01
        • 2023-01-25
        • 2021-12-22
        相关资源
        最近更新 更多