awk根据file1

时间:2016-04-05 16:15:40

标签: awk

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

  1. $1file1
  2. 之间找到匹配的字段file2字符串
  3. 如果$1字符串匹配,那么$2 in file1除以$3 in file2(即x为3个有意义的数字四舍五入)
  4. x乘以100
  5. 从100减去每个x,即%
  6. 文件1

    USH2A 21
    GIT1 357
    PALB2 3
    

    file2的

    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%
    

    我在代码中逐行,可以看到我已经收到错误。谢谢你:)。

2 个答案:

答案 0 :(得分:1)

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函数。

答案 1 :(得分:1)

$ 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%