awk根据文件1中的匹配项划分字段

awk根据文件1中的匹配项划分字段,awk,Awk,我正在尝试使用awk执行以下步骤 在file1和file2 如果$1字符串匹配,则将文件1中的$2除以文件2中的$3(即x,向上舍入3个显著数字) x乘以100 从100中减去每个x,即% file1 USH2A 21 GIT1 357 PALB2 3 文件2 GIT1 21 3096 USH2A 71 17718 PALB2 13 3954 awk awk 'NR==FNR{a[$1]=$1;next;}{if ($1 in a) print $1, $2/a[$3];else print

我正在尝试使用
awk
执行以下步骤

  • file1
    file2
  • 如果
    $1
    字符串匹配,则将文件1中的
    $2
    除以文件2中的
    $3
    (即x,向上舍入3个显著数字)
  • x乘以100
  • 从100中减去每个x,即
    %
  • file1

    USH2A 21
    GIT1 357
    PALB2 3
    
    文件2

    GIT1 21 3096
    USH2A 71 17718
    PALB2 13 3954
    
    awk

    awk 'NR==FNR{a[$1]=$1;next;}{if ($1 in a) print $1, $2/a[$3];else print;}' file2 file1 > test
    awk: cmd. line:1: (FILENAME=search FNR=2) fatal: division by zero attempted
    
    awk 'NR==FNR{a[$1]=$1;next;}{if ($1 in a) print $1, $2/a[$3];else print;}' file1 file2 > test
    awk: cmd. line:1: (FILENAME=search FNR=1) fatal: division by zero attempted
    
    示例

    USH2A match is found so (21/17718)*100 = 0.11  and 100-0.11 = 99.99%
    GIT1 match is found so (357/3096)*100 = 11.53 and 100-11.53 = 88.47%
    PALB2 match is found so (3/3954) *100 =  0.07 and 100-0.7 = 99.93%
    

    我在代码中一行一行地写,可以看到我已经得到了错误。谢谢:)。

    awk
    救援

    $ awk 'function ceil(v) {return int(v)==v?v:int(v+1)}
            NR==FNR{f1[$1]=$2; next} 
           $1 in f1{print $1, ceil(10000*(1-f1[$1]/$3))/100 "%"}' file1 file2
    
    GIT1 88.47%
    USH2A 99.89%
    PALB2 99.93%
    

    请注意,
    awk
    中没有为该任务定义一个
    ceil
    函数的汇总。

    awk
    救命

    $ awk 'function ceil(v) {return int(v)==v?v:int(v+1)}
            NR==FNR{f1[$1]=$2; next} 
           $1 in f1{print $1, ceil(10000*(1-f1[$1]/$3))/100 "%"}' file1 file2
    
    GIT1 88.47%
    USH2A 99.89%
    PALB2 99.93%
    
    $ cat tst.awk
    NR==FNR { a[$1]=$3; next }
    $1 in a {
        x = (a[$1] ? ($2*100)/a[$1] : 0)
        printf "%s match is found so (%d/%d) *100 =  %.2f and 100-%.2f = %.2f%%\n", $1, $2, a[$1], x, x, 100-x
    }
    
    $ awk -f tst.awk file2 file1
    USH2A match is found so (21/17718) *100 =  0.12 and 100-0.12 = 99.88%
    GIT1 match is found so (357/3096) *100 =  11.53 and 100-11.53 = 88.47%
    PALB2 match is found so (3/3954) *100 =  0.08 and 100-0.08 = 99.92%
    

    请注意,
    awk
    中没有为该任务定义的
    ceil
    函数的舍入。

    您需要防止
    a[$3]
    为零或空(例如,如果第二个文件中的$3值在第一个文件中不作为$1值存在)。类似于
    print$1,(a[$3]?$2/a[$3]:“NaN”)
    就可以了。不清楚您为什么使用
    a[$3]
    而不是
    $3
    。您对USH2A的计算是错误的。您需要防止
    a[$3]
    为零或空(例如,如果第二个文件中的$3值在第一个文件中不作为$1值存在)。类似于
    print$1,(a[$3]?$2/a[$3]:“NaN”)
    就可以了。不清楚你为什么用
    a[$3]
    而不是
    $3
    。你对USH2A的计算是错误的。非常感谢:)非常感谢:)
    $ cat tst.awk
    NR==FNR { a[$1]=$3; next }
    $1 in a {
        x = (a[$1] ? ($2*100)/a[$1] : 0)
        printf "%s match is found so (%d/%d) *100 =  %.2f and 100-%.2f = %.2f%%\n", $1, $2, a[$1], x, x, 100-x
    }
    
    $ awk -f tst.awk file2 file1
    USH2A match is found so (21/17718) *100 =  0.12 and 100-0.12 = 99.88%
    GIT1 match is found so (357/3096) *100 =  11.53 and 100-11.53 = 88.47%
    PALB2 match is found so (3/3954) *100 =  0.08 and 100-0.08 = 99.92%