比较 2 个不同文件中的 2 列

比较 2 个不同文件中的 2 列

我需要根据第一列减去 2 个文件之间的第二列和第三列,并且不显示 diff_column_2(DIFF-IO)=diff_column_3(DIFF-SELECT)=0 的行。请注意,顺序可以是随机的,除了两个文件中都存在的最后一行,并且应保留在输出中的 END 处。

ref_file

    testing 20 10
    jobs 15 20
    issues 0 1
    work 15 25
    Total 50 56

head_file

    testing 20 10
    jobs 15 30
    work 12 25
    games 1 2
    Total 48 67

期望的输出:

TABLE,REF-IO,HEAD-IO,DIFF-IO,REF-SELECT,HEAD-SELECT,DIFF-SELECT
jobs,15,15,0,20,30,-10
work,15,12,3,25,25,0
games,0,1,-1,0,2,-2
issues,0,0,0,1,0,1
Total,50,48,2,56,67,-11

答案1

awk '
    BEGIN {
        print "TABLE,REF-IO,HEAD-IO,DIFF-IO,REF-SELECT,HEAD-SELECT,DIFF-SELECT"
        OFS = ", "
    }
    FNR==NR {
        A[$1]=$2
        B[$1]=$3
        next
    }
    {
        if (!($1 in A)) {
            A[$1] = B[$1] = 0
        }
        diff_io = $2 - A[$1]
        diff_sel= $3 - B[$1]
        C[$1] = 1
    }
    diff_io || diff_sel {
        if (first) {
            print line
        }
        first = 1
        line = $1 OFS $2 OFS A[$1] OFS diff_io OFS $3 OFS B[$1] OFS diff_sel
    }
    END {
        for (name in A) {
            if (!(name in C)) {
                print name, 0, A[name], -A[name], 0, B[name], -B[name]
            }
        }
        print line
    }
    ' head_file ref_file

要简短打字,您可以通过以下方式完成任务join

join -a1 -a2 -e0 <(sort head_file) <(sort ref_file) -o0,1.2,2.2,0,1.3,2.3 |
awk '
    BEGIN {
        print "TABLE,REF-IO,HEAD-IO,DIFF-IO,REF-SELECT,HEAD-SELECT,DIFF-SELECT"
        OFS = ", "
    }
    {
        $4=$2-$3
        $7=$5-$6
    }
    /Total/ {
        end=$0
        next
    }
    $4!=0 || $7!=0;
    END {
        print end
    }'

相关内容