* *编辑:好的,到目前为止,我已经尝试实施每个人的建议。
-我在每个变量 "$1" 和 "$codon" 周围添加了引号以避免空格。
-我已将 -ioc 标志添加到 grep 以避免大写。
-我尝试使用 tr -d' ',但这会导致运行时错误,因为它说 -d' ' 是无效选项。
不幸的是,我仍然看到同样的问题。或者另一个问题,它告诉我每个密码子只出现一次。这是另一种错误。
感谢到目前为止所做的一切 - 我仍然对新想法持开放态度。我在下面更新了我的代码。* *
我有这个 bash 脚本,它应该计算给定文件中 (ACGT) 的所有排列。
脚本的一行没有给我想要的结果,我不知道为什么 - 特别是因为我可以在命令提示符下输入完全相同的代码行并获得想要的结果。
在命令提示符下执行的行是:
cat dnafile | grep -o GCT | wc -l
这一行告诉我正则表达式“GCT”在文件 dnafile 中出现了多少次。当我运行这个命令时,我得到的结果是 10(这是准确的)。
在代码本身中,我运行同一命令的修改版本:
cat $1 | grep -o $codon | wc -l
其中 $1 是文件名,$codon 是 3 个字母组合。当我从程序中运行它时,我得到的答案总是 0(这绝对不准确)。
我希望你们中的一位优秀绅士能够启发这个迷失的灵魂,了解为什么它没有按预期工作。
非常非常感谢你!
我的代码:
#!/bin/bash
#countcodons <dnafile> counts occurances of each codon in sequence contained within <dnafile>
if [[ $# != 1 ]]
then echo "Format is: countcodons <dnafile>"
exit
fi
nucleos=(a c g t)
allCods=()
#mix and match nucleotides to create all codons
for x in {0..3}
do
for y in {0..3}
do
for z in {0..3}
do
perm=${nucleos[$x]}${nucleos[$y]}${nucleos[$z]}
allCods=("${allCods[@]}" "$perm")
done
done
done
#for each codon, use grep to count # of occurances in file
len=${#allCods[*]}
for (( n=0; n<len; n++ ))
do
codon=${allCods[$n]}
occs=`cat "$1" | grep -ioc "$codon" | wc -l`
echo "$codon appears: $occs"
# if (( $occs > 0 ))
# then
# echo "$codon : $occs"
# fi
done
exit