【问题标题】:Awk for each column compare consecutive rows and add value每列的awk比较连续行并添加值
【发布时间】:2018-03-21 00:51:20
【问题描述】:

我搜索了相关问题,但找不到适合我的问题的答案。例如,我有一个 6 行 3 列的文件。

id  sample1 sample2  sample3
6   +/+      +/+      +/-
16  -/-      +/+      +/+
20  +/-      +/+      +/+
21  +/-      +/+      +/+
22  +/+      +/+      -/-
25  +/+      +/+      +/+   

对于每一列,我需要将一行的字符串与下一行的字符串进行比较,并根据值,用AWK报告一个数字,基于以下比较表(无论field1和字段2):

field1   field2   value
 +/+      +/+       0
 +/+      +/-       0.5
 +/-      +/-       1
 +/+      -/-       2
 +/-      -/-       2.5
 -/-      -/-       4

因此期望的输出是:

id  sample1 sample2 sample3 result1  result2    result3
6   +/+      +/+      +/-     2        0           0.5
16  -/-      +/+      +/+     2.5      0           0
20  +/-      +/+      +/+     1        0           0
21  +/-      +/+      +/+     0.5      0           2
22  +/+      +/+      -/-     0 0      2
25  +/+      +/+      +/+   

任何帮助将不胜感激。

【问题讨论】:

  • 你有没有尝试过?

标签: awk


【解决方案1】:

另一个类似的awk

$ awk 'NR==FNR {a[$1,$2]=a[$2,$1]=$3; next}
       FNR==1  {print $0,"result1","result2","result3"; next}
               {print f0, a[f[2],$2], a[f[3],$3], a[f[4],$4]; 
                f0=$0; split($0,f)}
       END     {print}' score file | column -t

id  sample1  sample2  sample3  result1  result2  result3
6   +/+      +/+      +/-      2        0        0.5
16  -/-      +/+      +/+      2.5      0        0
20  +/-      +/+      +/+      1        0        0
21  +/-      +/+      +/+      0.5      0        2
22  +/+      +/+      -/-      0        0        2
25  +/+      +/+      +/+

【讨论】:

  • 很好的解决方案。我只是想弄清楚它是如何工作的,如果有可变数量的列。感谢您的帮助。
  • 对于可变数量的列,您需要用 for loop 和 printf 替换 print 语句。
【解决方案2】:

awk解决方案:

awk 'NR == FNR{ if (NR > 1) a[$1$2] = $3; next }
     FNR == 1{ print $0, "result1\tresult2\tresult3"; next }
     id{ 
         print id, s2, s3, s4, 
               (a[$2 s2] == ""? a[s2 $2] : a[$2 s2]),
               (a[$3 s3] == ""? a[s3 $3] : a[$3 s3]),
               (a[$4 s4] == ""? a[s4 $4] : a[$4 s4]) 
     }
     { id = $1; s2 = $2; s3 = $3; s4 = $4 }
     END{ print $0 }' table.txt OFS='\t' data.txt | column -t

输出:

id  sample1  sample2  sample3  result1  result2  result3
6   +/+      +/+      +/-      2        0        0.5
16  -/-      +/+      +/+      2.5      0        0
20  +/-      +/+      +/+      1        0        0
21  +/-      +/+      +/+      0.5      0        2
22  +/+      +/+      -/-      0        0        2
25  +/+      +/+      +/+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多