Issue
I want to be able to show the output in sorted in order of decreasing frequency. If two or more codons have the same frequency, the script should break the tie using the alphabetical order of the codons. In this example, cag and gtt each occur just once, but because c precedes g, cag comes before gtt above.
when I run my script
cat test
aacgtttgtaaccagaactgt
./histogram test
3 aac
1 gtt
2 tgt
1 cag
but instead, I want to be able to do
./histogram test
3 aac
2 tgt
1 cag
1 gtt
Here what I did
declare -a codons=(aac gtt tgt cag)
for items in ${codons[@]}
do
count=$( grep -o $items $1 | sort -k1,1nr -k2,2 | wc -l)
# count=$( grep -o $items $1 | uniq -c | wc -l | sort -k1,1nr -k2,2 )
echo "$count $items "
done
Solution
Just sort the output.
./histogram test | sort -k1,1nr -k2,2
If you want to include the sort
into your script you can append it after the done
of your loop:
...
done | sort -k1,1nr -k2,2
By the way: sort | wc -l
is wasted time, since sort
does not change the number of lines. Just use wc -l
.
For your example output of ./histogram test
this should print
3 aac
2 tgt
1 cag
1 gtt
Answered By - Socowi Answer Checked By - Mary Flores (WPSolving Volunteer)