![](/img/trans.png)
[英]Linux command executed from C code system() function giving different result as compared to when executed from Terminal
[英]Why is this command within my code giving different result than the same command in terminal?
* *編輯:好的,到目前為止,我已經嘗試實現每個人的建議。
-我在每個變量“ $ 1”和“ $ codon”周圍加上了引號,以避免出現空格。
-我在grep中添加了-ioc標志,以避免出現大寫字母。
-我嘗試使用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
您正在以小寫形式生成序列。 您的代碼針對的是gct,而不是GCT。 您要將-i開關添加到grep。 嘗試:
occs=`grep -ioc $codon $1`
嘗試:
occs=`cat $1 | grep -o $codon | wc -l | tr -d ' '`
問題是wc
使輸出縮進,因此$occs
有很多空格。
您的邏輯倒退了-您不必為每個密碼子讀取一次輸入文件,您只需要讀取一次並檢查每個密碼子的每一行。
您沒有提供任何樣本輸入或預期輸出,因此未經測試,但是像這樣的方法是正確的:
awk '
BEGIN {
nucleosStr="a c g t"
split(nucleosStr,nucleos)
#mix and match nucleotides to create all codons
for (x in nucleos) {
for (y in nucleos) {
for (z in nucleos) {
perm = nucleos[x] nucleos[y] nucleos[z]
allCodsStr = allCodsStr (allCodsStr?" ":"") perm
}
}
}
split(allCodsStr,allCods)
}
{
#for each codon, count # of occurances in file
for (n in allCods) {
codon = allCods[n]
if ( tolower($0) ~ codon ) {
occs[n]++
}
}
}
END {
for (n in allCods) {
printf "%s appears: %d\n", allCods[n], occs[n]
}
}
' "$1"
如果文件很大,我希望您會看到使用這種方法的巨大性能改進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.