1

I have a space delimited text file:

NC_005943.1 RefSeq CDS 3259 4213 . + 0 gene_id "ND1";
NC_005943.1 RefSeq CDS 4421 5462 . + 0 gene_id "ND2";
NC_005943.1 RefSeq CDS 5850 7418 . + 0 gene_id "COX1";
NC_005943.1 RefSeq CDS 7532 8215 . + 0 gene_id "COX2";
NC_005943.1 RefSeq CDS 8357 8563 . + 0 gene_id "ATP8";

I need to replace the string "gene ID" in $9 with "transcript_id".

Doing:

awk -F " " '{ if ($9 == "gene_id") $9="transcript_id";}2' OFS=, genes_2.gtf | head

Gives me:

NC_005943.1,RefSeq,CDS,3259,4213,.,+,0,transcript_id,"ND1";
NC_005943.1,RefSeq,CDS,4421,5462,.,+,0,transcript_id,"ND2";
NC_005943.1,RefSeq,CDS,5850,7418,.,+,0,transcript_id,"COX1";
NC_005943.1,RefSeq,CDS,7532,8215,.,+,0,transcript_id,"COX2";
NC_005943.1,RefSeq,CDS,8357,8563,.,+,0,transcript_id,"ATP8";

...comma separated delimiter.

I need to preserve the space delimiter. I know it's something in awk -F but have had trouble changing the flag to a space.

thanks so much.

3
  • try changing OFS=, to OFS=" "
    – iruvar
    Sep 26 '20 at 4:40
  • 1
    @iruvar Or removing OFS=, since OFS=" " is the default. Sep 26 '20 at 6:00
  • -F " " is setting FS to the default value " " it'd have anyway so that statement is useless and then OFS=, is setting OFS to a value you don't want it to have (,) instead of the default value that you actually DO want it to have, " ". So, just don't do that, remove both -F " " and OFS=,.
    – Ed Morton
    Sep 26 '20 at 11:34
1

This should work :

awk '{ $9 = "transcript_id"; print }' genes_2.gtf
1
  • 2
    Or awk '$9 == "gene_id" { $9 = "transcript_id" }; 1' file.gtf if the modification needs to happen only when the 9th field is exactly gene_id.
    – Kusalananda
    Sep 26 '20 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.