Skip to main content
added 1 character in body
Source Link
terdon
  • 10.6k
  • 5
  • 23
  • 48

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.

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.

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.
Source Link
terdon
  • 10.6k
  • 5
  • 23
  • 48

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.