bash - Why is this command within my code giving different result than the same command in terminal? -
**edit: okay, i've tried implementing everyone's advice far.
-i've added quotes around each variable "$1" , "$codon" avoid whitespace.
-i've added -ioc flag grep avoid caps.
-i tried using tr -d' ', leads runtime error because says -d' ' invalid option.
unfortunately still seeing same problem. or different problem, tells me every codon appears once. different kind of wrong.
thanks far - i'm still open new ideas. i've updated code below.**
i have bash script supposed count permutations of (a c g t) in given file.
one line of script not giving me desired result , don't know why - because can enter exact same line of code in command prompt , desired result.
the line, executed in command prompt, is:
cat dnafile | grep -o gct | wc -l this line tells me how many times regular expression "gct" appears in file dnafile. when run command result 10 (which accurate).
in code itself, run modified version of same command:
cat $1 | grep -o $codon | wc -l where $1 file name, , $codon 3-letter combination. when run within program, answer 0 (which decidedly not accurate).
i hoping 1 of fine gents enlighten lost soul why not working expected.
thank very, much!
my code:
#!/bin/bash #countcodons <dnafile> counts occurances of each codon in sequence contained within <dnafile> if [[ $# != 1 ]] echo "format is: countcodons <dnafile>" exit fi nucleos=(a c g t) allcods=() #mix , match nucleotides create codons x in {0..3} y in {0..3} z in {0..3} perm=${nucleos[$x]}${nucleos[$y]}${nucleos[$z]} allcods=("${allcods[@]}" "$perm") done done done #for each codon, use grep count # of occurances in file len=${#allcods[*]} (( n=0; n<len; n++ )) codon=${allcods[$n]} occs=`cat "$1" | grep -ioc "$codon" | wc -l` echo "$codon appears: $occs" # if (( $occs > 0 )) # # echo "$codon : $occs" # fi done exit
you're generating sequences in lowercase. code greps gct, not gct. want add -i switch grep. try:
occs=`grep -ioc $codon $1`
Comments
Post a Comment