You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用awk/sed在GTF文件末尾追加列5与列4的差值?

Fixing Your Awk Command to Append Columns in GENCODE GTF Files

Let's get this sorted out—your original awk command was replacing an existing column instead of appending the new value, which is why it didn't work as expected. Here's how to fix it:

The Correct Awk Command

awk 'NR>=6 {$0 = $0 " " $5 - $4} 1' gencode.gtf > output.txt

Breakdown of the Command:

  • NR>=6: Targets all lines starting from the 6th row (skipping your 5-line header).
  • $0 = $0 " " $5 - $4: Takes the entire existing line ($0), adds a space separator, then appends the result of subtracting column 4 from column 5. This modifies the line to include your new value at the end instead of overwriting an existing column.
  • 1: A shorthand in awk that means "print the current line". For lines 1-5, this just prints the original header; for lines 6+, it prints the modified line with the appended difference.

Why Your Original Command Failed

Your initial command awk 'NR>=6 {$10=$5-$4} 1' was overwriting the 10th field (which in your example is gene_id) with the calculation result. GTF files have a fixed structure where each field serves a specific purpose, so replacing an existing field breaks the file format—we need to add a new field instead.

Example Output

For your sample line:

chr1 HAVANA gene 11869 14409 . + . gene_id "ENSG00000223972.5"; ...

The command will produce:

chr1 HAVANA gene 11869 14409 . + . gene_id "ENSG00000223972.5"; ... 2540

Alternative (More Explicit) Version

If you prefer more readable code, this does the exact same thing:

awk '{
    if (NR >= 6) {
        print $0, $5 - $4
    } else {
        print $0
    }
}' gencode.gtf > output.txt

内容的提问来源于stack exchange,提问作者Gawain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 21:17:58