<p>I don't see how your <code>awk</code> command would work since you're using whitespace as a field delimiter. In any case, you can use a much shorter perl command for this:</p>

<pre><code>$ perl -lne 'print "@m" if @m=(/((?:transcript_id|gene_name|transcript_name)\s+\S+)/g);' Homo_sapiens.GRCh37.70.gtf 
transcript_id "ENSG00000223972.5"; gene_name "DDX11L1"; transcript_name "DDX11L1";
transcript_id "ENST00000456328.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-002";
transcript_id "ENST00000456328.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-002";
transcript_id "ENST00000456328.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-002";
transcript_id "ENST00000456328.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-002";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";
</code></pre>

<p>The scriptlet will print the contents of the <code>@m</code> array if this line matches what you need. The regex magic is:</p>

<ul>
<li><code>(?: )</code>: this is a non-capturing group. We need this so that <code>@m</code> only holds what is inside the <em>capturing</em> parentheses (see below).</li>
<li><code>(  (?:transcript_id|gene_name|transcript_name)\s+\S+)</code> : the outer parentheses will capture everything matched: one of <code>transcript_id|gene_name|transcript_name</code> followed by 1 or more whitespace characters (<code>\s+</code>) and one or more non-whitespace characters.</li>
<li><code>print @m if @m=...</code> : only print if the match was successful.</li>
</ul>

CC BY-SA 4.0
2018-12-06T13:04:14.947Z
i was in hurry and dire need almost all the work i do it in R ,i tried it in R due to the size of the gtf it just froze ,  looks like I need to learn some basics of awk along with perl that will make my life simpler...thank you very much i will use your code...
kcm
CC BY-SA 4.0
2018-12-06T13:24:46.543Z
that was super fast in takes time with awk but this is the time it took 
real 0m16.112s
user 0m14.653s
sys 0m0.693s
kcm
CC BY-SA 4.0
2018-12-06T12:59:50.387Z
2018-12-06T20:23:02.830Z
2018-12-06T20:23:02.830Z
terdon
<p>I used this previously to get ensembl ID and its gene name from the gencode gtf annotation file </p>

<p>This was the <a href="https://www.biostars.org/p/122012/" rel="nofollow noreferrer">script</a> I used </p>

<pre><code>awk '{                                
    for (i = 1; i &lt;= NF; i++) {
        if (<span class="math-container">$i ~ /gene_id|gene_name/) {
        printf "%s ", $</span>(i+1)
        }
    }
    print ""
}' Homo_sapiens.GRCh37.70.gtf | sed -e 's/"//g' -e 's/;//g' -e 's/ /\t/' | sort -k1,1 | uniq &gt; Homo_sapiens.GRCh37.70.txt
</code></pre>

<p>And the input file:</p>

<pre><code>##description: evidence-based annotation of the human genome (GRCh38), version 21 (Ensembl 77)
##provider: GENCODE
##contact: gencode@sanger.ac.uk
##format: gtf
##date: 2014-09-29
chr1    HAVANA  gene    11869   14409   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENSG00000223972.5"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1"; level 2; havana_gene "OTTHUMG00000000961.2";
chr1    HAVANA  transcript  11869   14409   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "processed_transcript"; transcript_status "KNOWN"; transcript_name "DDX11L1-002"; level 2; tag "basic"; transcript_support_level "1"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000362751.1";
chr1    HAVANA  exon    11869   12227   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "processed_transcript"; transcript_status "KNOWN"; transcript_name "DDX11L1-002"; exon_number 1; exon_id "ENSE00002234944.1"; level 2; tag "basic"; transcript_support_level "1"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000362751.1";
chr1    HAVANA  exon    12613   12721   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "processed_transcript"; transcript_status "KNOWN"; transcript_name "DDX11L1-002"; exon_number 2; exon_id "ENSE00003582793.1"; level 2; tag "basic"; transcript_support_level "1"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000362751.1";
chr1    HAVANA  exon    13221   14409   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "processed_transcript"; transcript_status "KNOWN"; transcript_name "DDX11L1-002"; exon_number 3; exon_id "ENSE00002312635.1"; level 2; tag "basic"; transcript_support_level "1"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000362751.1";
chr1    HAVANA  transcript  12010   13670   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
chr1    HAVANA  exon    12010   12057   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 1; exon_id "ENSE00001948541.1"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
chr1    HAVANA  exon    12179   12227   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 2; exon_id "ENSE00001671638.2"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
chr1    HAVANA  exon    12613   12697   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 3; exon_id "ENSE00001758273.2"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
chr1    HAVANA  exon    12975   13052   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 4; exon_id "ENSE00001799933.2"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
</code></pre>

<p>What I'm trying to do now is replace the gene_ID with transcript_ID and the gene_name with transcript_name but I'm doing something wrong as I'm not getting the transcript_ID but the gene Ensembl ID instead. I'm not sure if I'm choosing the right column for parsing the transcript_ID and name.</p>

<p>So I'm expecting to get this</p>

<pre><code>transcript_id "ENST00000450305.2";  gene_name "DDX11L1";transcript_name "DDX11L1-001"
</code></pre>

<p>Any help or suggestion would be highly appreciated </p>

CC BY-SA 4.0
2018-12-06T07:48:23.593Z
I get the Ensembl transcript ID and the transcript name (e.g., "ENST00000320153 DDI2-201"). What is the output you're getting?
Devon Ryan
CC BY-SA 4.0
2018-12-06T09:50:19.803Z
im getting ENSG and ENST  ids both along with gene name, did you run the same thing?im doing something wrong perhaps
kcm
CC BY-SA 4.0
2018-12-06T10:02:10.800Z
I'm running the exact same thing with `/transcript_id|transcript_name/` rather than `/gene_id|gene_name/`.
Devon Ryan
CC BY-SA 4.0
2018-12-06T10:06:11.227Z
okay ,yes im doing the same thing...but my output is everything combined gene as well as transcript ID with names if you can look into my file may be you can tell https://drive.google.com/open?id=1Frzy0R1j1Vncog6LP5E8N2TIJFBZsS3z what i need is can i get only transcript ID and name...
kcm
<gtf><text-processing>
Parsing gtf file for transcript ID and transcript name
# Parsing gtf file for transcript ID and transcript name

(Asked by: username_0 on 2018-12-05)

I used this previously to get ensembl ID and its gene name from the gencode gtf annotation file 
This was the script I used 
awk '{                                
    for (i = 1; i <= NF; i++) {
        if ($i ~ /gene_id|gene_name/) {
        printf "%s ", $(i+1)
        }
    }
    print ""
}' Homo_sapiens.GRCh37.70.gtf | sed -e 's/"//g' -e 's/;//g' -e 's/ /\t/' | sort -k1,1 | uniq > Homo_sapiens.GRCh37.70.txt

And the input file:
##description: evidence-based annotation of the human genome (GRCh38), version 21 (Ensembl 77)
##provider: GENCODE
##contact: gencode@sanger.ac.uk
##format: gtf
##date: 2014-09-29
chr1    HAVANA  gene    11869   14409   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENSG00000223972.5"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1"; level 2; havana_gene "OTTHUMG00000000961.2";
chr1    HAVANA  transcript  11869   14409   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "processed_transcript"; transcript_status "KNOWN"; transcript_name "DDX11L1-002"; level 2; tag "basic"; transcript_support_level "1"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000362751.1";
chr1    HAVANA  exon    11869   12227   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "processed_transcript"; transcript_status "KNOWN"; transcript_name "DDX11L1-002"; exon_number 1; exon_id "ENSE00002234944.1"; level 2; tag "basic"; transcript_support_level "1"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000362751.1";
chr1    HAVANA  exon    12613   12721   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "processed_transcript"; transcript_status "KNOWN"; transcript_name "DDX11L1-002"; exon_number 2; exon_id "ENSE00003582793.1"; level 2; tag "basic"; transcript_support_level "1"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000362751.1";
chr1    HAVANA  exon    13221   14409   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "processed_transcript"; transcript_status "KNOWN"; transcript_name "DDX11L1-002"; exon_number 3; exon_id "ENSE00002312635.1"; level 2; tag "basic"; transcript_support_level "1"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000362751.1";
chr1    HAVANA  transcript  12010   13670   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
chr1    HAVANA  exon    12010   12057   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 1; exon_id "ENSE00001948541.1"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
chr1    HAVANA  exon    12179   12227   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 2; exon_id "ENSE00001671638.2"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
chr1    HAVANA  exon    12613   12697   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 3; exon_id "ENSE00001758273.2"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
chr1    HAVANA  exon    12975   13052   .   +   .   gene_id "ENSG00000223972.5"; transcript_id "ENST00000450305.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 4; exon_id "ENSE00001799933.2"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; tag "basic"; transcript_support_level "NA"; havana_gene "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";

What I'm trying to do now is replace the gene_ID with transcript_ID and the gene_name with transcript_name but I'm doing something wrong as I'm not getting the transcript_ID but the gene Ensembl ID instead. I'm not sure if I'm choosing the right column for parsing the transcript_ID and name.
So I'm expecting to get this
transcript_id "ENST00000450305.2";  gene_name "DDX11L1";transcript_name "DDX11L1-001"

Any help or suggestion would be highly appreciated

**Tags:** gtf, text-processing
> **Comments:**
> **username_1**: I get the Ensembl transcript ID and the transcript name (e.g., "ENST00000320153 DDI2-201"). What is the output you're getting?  
> **username_0**: im getting ENSG and ENST  ids both along with gene name, did you run the same thing?im doing something wrong perhaps  
> **username_1**: I'm running the exact same thing with `/transcript_id|transcript_name/` rather than `/gene_id|gene_name/`.  
> **username_0**: okay ,yes im doing the same thing...but my output is everything combined gene as well as transcript ID with names if you can look into my file may be you can tell https://drive.google.com/open?id=1Frzy0R1j1Vncog6LP5E8N2TIJFBZsS3z what i need is can i get only transcript ID and name...  

---

## 1 answers

### Answer by username_2 on 2018-12-06. Score: 3 | Accepted answer

I don't see how your awk command would work since you're using whitespace as a field delimiter. In any case, you can use a much shorter perl command for this:
$ perl -lne 'print "@m" if @m=(/((?:transcript_id|gene_name|transcript_name)\s+\S+)/g);' Homo_sapiens.GRCh37.70.gtf 
transcript_id "ENSG00000223972.5"; gene_name "DDX11L1"; transcript_name "DDX11L1";
transcript_id "ENST00000456328.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-002";
transcript_id "ENST00000456328.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-002";
transcript_id "ENST00000456328.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-002";
transcript_id "ENST00000456328.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-002";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";
transcript_id "ENST00000450305.2"; gene_name "DDX11L1"; transcript_name "DDX11L1-001";

The scriptlet will print the contents of the @m array if this line matches what you need. The regex magic is:

(?: ): this is a non-capturing group. We need this so that @m only holds what is inside the capturing parentheses (see below).
(  (?:transcript_id|gene_name|transcript_name)\s+\S+) : the outer parentheses will capture everything matched: one of transcript_id|gene_name|transcript_name followed by 1 or more whitespace characters (\s+) and one or more non-whitespace characters.
print @m if @m=... : only print if the match was successful.

> **Comments:**
> **username_0**: i was in hurry and dire need almost all the work i do it in R ,i tried it in R due to the size of the gtf it just froze ,  looks like I need to learn some basics of awk along with perl that will make my life simpler...thank you very much i will use your code...  
> **username_0**: that was super fast in takes time with awk but this is the time it took  real 0m16.112s user 0m14.653s sys 0m0.693s  

---

