Thanks! That's almost exactly what I'm looking for, except even when I change the "print $13" at the end to "print $0", I still don't get the entire line, only the 9th column. Doing the cut -f9 so early in the process means you discarded the rest of the line, right? How do I get it to print the entire line?
I'm trying to only print lines in a GTF file with the "tag" field "appris_principal" AND if that tag doesn't exist, then the ones tagged with "appris_candidate_longest" are selected, for any given gene.
I think I can code it up in python but there must be a way to do it in awk?
1 answer
Hi, Since you want to try in awk, I came up with this one-liner--
cat your_gtf_file | grep -v "#" | cut -d " " -f9| cut -d";" -f1-18 | awk '{ if ($0~"appris_principle")print $0; else if ($0~"appris_candidate_longest")print $13}'
Let me know if this works!
Thanks
Oh sorry, it's also missing the condition: for each gene, meaning the if and else if should apply to each gene (for each unique string after the gene_name field), rather than each line.
I am glad you tried. So you mean you want to print the whole line which satisfies the condition, including all the columns from start?
If that's the case, this will work
awk '{ if ($0~"appris_principle")print $0; else if ($0~"appris_candidate_longest")print $0}' your.gtf
I am not sure why you want to apply after each gene_name and not to whole line.
Could you please post the output example as well as the sample input so that I might get it right, the way you want?
Thanks
Log in to answer this question.
Why not
grep? That might be the easiest and the quickest.Oh yeah let's not forget grep. But I'm not sure how to make the condition if appris_principal doesn't exist in this line, check whether appris_candidate_longest exists. I neither, don't print.
Extract matching lines:
Extract non-matching lines:
... ...
Check for lines that have appris_candidate_longest AND NOT appris_principal:
honestly I would just do it in Python. Use
csv.DictReader. Shouldnt take more than a dozen lines.