问题
I have a multi-column GTF file, where each row has different number of columns:
chr1 Cufflinks exon 12659 12721 . + . gene_id "XLOC_000001"; transcript_id "TCONS_00000001"; exon_number "1"; oId "CUFF.3.1"; class_code "u"; tss_id "TSS1";
chr1 Cufflinks exon 13221 16604 . + . gene_id "XLOC_000001"; transcript_id "TCONS_00000001"; exon_number "2"; oId "CUFF.3.1"; class_code "u"; tss_id "TSS1";
chr1 Cufflinks exon 29554 30039 . + . gene_id "XLOC_000002"; transcript_id "TCONS_00000002"; exon_number "1"; gene_name "MIR1302-11"; oId "ENST00000473358"; nearest_ref "ENST00000473358"; class_code "="; tss_id "TSS2";
chr1 Cufflinks exon 30564 30667 . + . gene_id "XLOC_000002"; transcript_id "TCONS_00000002"; exon_number "2"; gene_name "MIR1302-11"; oId "ENST00000473358"; nearest_ref "ENST00000473358"; class_code "="; tss_id "TSS2";
chr1 Cufflinks exon 69091 70008 . + . gene_id "XLOC_000003"; transcript_id "TCONS_00000005"; exon_number "1"; gene_name "OR4F5"; oId "ENST00000335137"; nearest_ref "ENST00000335137"; class_code "="; tss_id "TSS4"; p_id "P1";
I only want columns matching the pattern 'gene_id "...";' 'transcript_id "...";' 'class_code "..";'
I tried removing the unwanted columns using:
sed -e 's/nearest_ref\s\"[A-Z]\{4\}[0-9]\{11\}\"\;//' -e 's/oId\s\"[A-Z|\.|0-9]*\"\;//' -e 's/gene_name\s\"[A-Z|0-9|\.|\-]*\"\;//' -e 's/contained_in\s\"[A-Z|\_|0-9]*\"\;//' -e 's/p_id*\s\".*\"\;//' merged.gtf > temp.gtf
But looks like there are many other unwanted columns in the file that I cannot see (the file is huge). How do I extract the desired columns and save it into another file?
回答1:
If you don't mind an extra trailing space, and my assumptions in my comment above are true, then the following should work:
awk '{
for (i = 1; i <= NF; i++) {
if ($i ~ /gene_id|transcript_id|class_code/) {
printf "%s %s ", $i, $(i + 1)
}
}
print ""
}' merged.gtf > temp.gtf
来源:https://stackoverflow.com/questions/20008680/extract-columns-with-values-matching-a-specific-pattern