OK, so I love my awk
, but yeah, it has precision issues, and unfortunately there's no easy way for me to install the multi-precision extensions known as gawkextlib
.
What I am doing is, I am working through problems on rosalind.info using shell one-liners. I find that it's not difficult for me to perform the required computations on DNA/RNA strands using these shell one-liners within the five minute timeframe set by the website.
Anyway, I'm stuck on this problem, but I always want to sharpen my knowledge of linux tools. In this case, I need to call bc
from awk
.
The bc
command should be:
bc <<< "scale=1000; $1/$2"
Where $1
and $2
are the two columns of text I am working with in awk
.
The awk
command is derived from some shell functions I wrote:
nucleic-line () {
sed 's/\(.\)/\1\n/g' < $@
}
gc-numeric-count () {
n=$(nucleic-line $@ | wc -l)
m=$(nucleic-line $@ | grep -v "[AT]" | wc -l)
echo $m $n
}
export -f gc-numeric-count
column-percent-count () {
for f in $@; do gc-numeric-count $f; done | awk '{a = $1/$2 | print a * 100}'
}
For my purposes, awk '{a = $1/$2 | print a * 100}'
is not precise enough. It gets the percentage of guanine and cytosine correct, but I need it to more decimal places than awk
can deliver. Like I said, I unfortunately can't install the gawkextlib
. I need arbitrary precision, so I need to use bc
. But I also want to be able to process columns, like I can in awk
.
So, how can I amend the last line of the last expression to use the bc
command on $1
and $2
?