为什么这个awk代码没有做我想做的事情?

时间:2017-04-24 17:03:10

标签: awk

我有这个文件:

$ head -n 4 badRegionFromHWE.merged
seqnames start end width strand
chr1 144118070 145868461 1750392 *
chr7 100049516 101110026 1060511 *
chr7 141508887 142999071 1490185 *
$

我想不打印标题行并打印由标签分隔的1,2,3列。所以我写了这个:

awk 'OFS="\t";NR>1{print$1,$2,$3}' badRegionFromHWE.merged | head
seqnames start end width strand
chr1 144118070 145868461 1750392 *
chr1    144118070   145868461
chr7 100049516 101110026 1060511 *
chr7    100049516   101110026
chr7 141508887 142999071 1490185 *
chr7    141508887   142999071

它没有做我想做的事情!

1 个答案:

答案 0 :(得分:2)

The assignment OFS="\t" evaluates to true (non-zero, non-empty) on every line, so it prints every line. You should enclose the expression in a BEGIN block:

awk 'BEGIN { OFS="\t" } NR > 1 { print$1, $2, $3 }' badRegionFromHWE.merged