如何在不更改数据格式的情况下用awk替换列

时间:2017-10-24 22:40:18

标签: bash awk

我有像这样的数据框

Chr1    Cufflinks   exon    768419  769441  .   .   .   gene_id "XLOC_008282"; transcript_id "TCONS_00014260"; exon_number "1"; oId "CUFF.87.1"; class_code "u"; tss_id "TSS9986";
Chr1    Cufflinks   exon    1519538 1520185 .   .   .   gene_id "XLOC_008283"; transcript_id "TCONS_00014261"; exon_number "1"; oId "CUFF.169.1"; class_code "u"; tss_id "TSS9987";
Chr1    Cufflinks   exon    1888251 1888965 .   .   .   gene_id "XLOC_008284"; transcript_id "TCONS_00014262"; exon_number "1"; oId "CUFF.206.1"; class_code "u"; tss_id "TSS9988";
Chr1    Cufflinks   exon    2865389 2865754 .   .   .   gene_id "XLOC_008285"; transcript_id "TCONS_00014263"; exon_number "1"; oId "CUFF.311.1"; class_code "u"; tss_id "TSS9989";
Chr1    Cufflinks   exon    5280222 5281681 .   .   .   gene_id "XLOC_008286"; transcript_id "TCONS_00014264"; exon_number "1"; oId "CUFF.522.1"; class_code "u"; tss_id "TSS9990";
Chr1    Cufflinks   exon    5325454 5325814 .   .   .   gene_id "XLOC_008287"; transcript_id "TCONS_00014265"; exon_number "1"; oId "CUFF.528.1"; class_code "u"; tss_id "TSS9991";
Chr1    Cufflinks   exon    5325939 5326211 .   .   .   gene_id "XLOC_008288"; transcript_id "TCONS_00014266"; exon_number "1"; oId "CUFF.529.1"; class_code "u"; tss_id "TSS9992";
Chr1    Cufflinks   exon    5733388 5733795 .   .   .   gene_id "XLOC_008289"; transcript_id "TCONS_00014267"; exon_number "1"; oId "CUFF.578.1"; class_code "u"; tss_id "TSS9993";
Chr1    Cufflinks   exon    5970769 5970971 .   .   .   gene_id "XLOC_008290"; transcript_id "TCONS_00014268"; exon_number "1"; oId "CUFF.602.1"; class_code "u"; tss_id "TSS9994";
Chr1    Cufflinks   exon    6140629 6141565 .   .   .   gene_id "XLOC_008291"; transcript_id "TCONS_00014269"; exon_number "1"; oId "CUFF.618.1"; class_code "u"; tss_id "TSS9995";

我想用.替换+的第7列。当我使用awk时,我得到了我想要的东西,但它改变了数据格式

  

awk' $ 7 =" +"' unstranded_clean.gtf

Chr1 Cufflinks exon 768419 769441 . + . gene_id "XLOC_008282"; transcript_id "TCONS_00014260"; exon_number "1"; oId "CUFF.87.1"; class_code "u"; tss_id "TSS9986";
Chr1 Cufflinks exon 1519538 1520185 . + . gene_id "XLOC_008283"; transcript_id "TCONS_00014261"; exon_number "1"; oId "CUFF.169.1"; class_code "u"; tss_id "TSS9987";
Chr1 Cufflinks exon 1888251 1888965 . + . gene_id "XLOC_008284"; transcript_id "TCONS_00014262"; exon_number "1"; oId "CUFF.206.1"; class_code "u"; tss_id "TSS9988";
Chr1 Cufflinks exon 2865389 2865754 . + . gene_id "XLOC_008285"; transcript_id "TCONS_00014263"; exon_number "1"; oId "CUFF.311.1"; class_code "u"; tss_id "TSS9989";
Chr1 Cufflinks exon 5280222 5281681 . + . gene_id "XLOC_008286"; transcript_id "TCONS_00014264"; exon_number "1"; oId "CUFF.522.1"; class_code "u"; tss_id "TSS9990";
Chr1 Cufflinks exon 5325454 5325814 . + . gene_id "XLOC_008287"; transcript_id "TCONS_00014265"; exon_number "1"; oId "CUFF.528.1"; class_code "u"; tss_id "TSS9991";
Chr1 Cufflinks exon 5325939 5326211 . + . gene_id "XLOC_008288"; transcript_id "TCONS_00014266"; exon_number "1"; oId "CUFF.529.1"; class_code "u"; tss_id "TSS9992";
Chr1 Cufflinks exon 5733388 5733795 . + . gene_id "XLOC_008289"; transcript_id "TCONS_00014267"; exon_number "1"; oId "CUFF.578.1"; class_code "u"; tss_id "TSS9993";
Chr1 Cufflinks exon 5970769 5970971 . + . gene_id "XLOC_008290"; transcript_id "TCONS_00014268"; exon_number "1"; oId "CUFF.602.1"; class_code "u"; tss_id "TSS9994";

如何保留数据格式?

2 个答案:

答案 0 :(得分:2)

在awk中,默认输出字段分隔符为空白。我猜你的输入数据有tab作为字段分隔符。为了确保在输入输出上只使用选项卡作为字段分隔符,请尝试:

[ 'a', 'p', 'p', 'y' ]

awk -F'\t' '{$7="+"} 1' OFS='\t' unstranded_clean.gtf 设置输出字段分隔符。

答案 1 :(得分:0)

如果您真的想要保留所有空白区域(不同数量的空格和制表符),则无法使用内部字段拆分机制 - 您需要自己完成:

BEGIN {
    ONEFLD="[^ \t]+[ \t]+"      # pattern to match a field and following white space
    for (i = 1; i < 7; i++)
        LHSPAT = LHSPAT ONEFLD
}
{
    if (match($0, LHSPAT) {
      lhs = substr($0, RSTART, RLENGTH)
      rem = substr($0, RLENGTH + 1)
    }
    if (match(rem, ONEFLD)) {
      field7 = substr(rem, RSTART, RLENGTH)
      sub(/[^ \t]+/, "+", field7)
      rhs = substr(rem, RLENGTH + 1)
    }
    print lhs field7 rhs
}