unix awk 开始语句

unix awk 开始语句

我试图在 awk 中使用 begin 语句,但不知何故,begin 语句打印在每条记录的顶部,而不是我想要的第一条记录的顶部。我不太清楚为什么,如果有人能给我一些关于我的代码出了什么问题的意见,我将不胜感激。

awk 'BEGIN { OFS="\t" }{print "MARKER\tCHR\tBP\tEA\tNEA\tEAF\tP\tOR\tSE\tOR_95L\tOR95U\tN\tN_CASES\tN_CONTROLS\tSTRAND\tINFO\tHWE_P\tIMPUTED"}FNR>16 && $45!="NA" && $9>=0.4 { if ($1=="---") print $2,"'"$chr"'",$4,$6,$5,$45,$42,$48,$43,$44,$18,$23,$28,"+",$9,$33,"0" ; else print $2,"'"$chr"'",$4,$6,$5,$45,$42,$48,$43,$44,$18,$23,$28,"+",$9,$33,"1" }' ./out/expected_dcct_1kg_only${chr}_${chunk}.res > ./temp/expected_dcct_1kg_chr${chr}_${chunk}.tmp

我希望看到这一行:

标记 Chr BP .... 1

仅在第一行,但不知何故,它似​​乎为每条记录打印。

这是输出的快照:

MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED
rs7002152   8   145000056   C   T   0.937422    0.984021    0.165311    0.71094 1.362   1304    79  1225    +   0.981763    0.309615    0
MARKER  CHR BP  EA  NEA EAF P   OR  SE  OR_95L  OR95U   N   N_CASES N_CONTROLS  STRAND  INFO    HWE_P   IMPUTED

答案1

那是因为你没有把你的print放在BEGIN.您在该BEGIN块中拥有的只是OFS="\t"。顺便说一句,这意味着您不需要添加"\t"到您的print通话中。所以,你想要的是(为了清楚起见,我稍微改变了格式):

awk 'BEGIN { OFS="\t"; 
            print "MARKER", "CHR", "BP", "EA", "NEA","EAF", "P","OR","SE", 
             "OR_95L","OR95U", "N","N_CASES", "N_CONTROLS", "STRAND","INFO", 
             "HWE_P","IMPUTED"
        }
FNR>16 && $45!="NA" && $9>=0.4 { 
    if ($1=="---") print $2,"'"$chr"'",$4,$6, $5,$45,$42,$48, 
                     $43,$44,$18,$23, $28,"+",$9,$33,"0" ; 
    else print $2,"'"$chr"'",$4,$6,$5,$45, $42,$48,$43,$44,$18, 
               $23,$28,"+",$9,$33,"1" 
}' ./out/expected_dcct_1kg_only${chr}_${chunk}.res > \
  ./temp/expected_dcct_1kg_chr${chr}_${chunk}.tmp

答案2

请记住,在 AWK 中,BEGIN 块中包含的所有内容都是在实际数据操作之前完成的。当您将一些命令放在 BEGIN 块之外时,它将开始影响您的数据处理。

END 块也是如此。处理完所有数据后执行 END 块。

相关内容