2つのawkファイルをマージする

2つのawkファイルをマージする

ncrとエネルギー方程式に基づいて利己的なノードを検出しようとしています。利己的なノードを検出しましたが、それぞれncrとエネルギーに基づいています。そして、これら2つの条件を組み合わせるのに問題があります。 (ノードのncrが30未満でエネルギーがしきい値未満の場合は、利己的なノードとして分類されます。)

利己的なノードのエネルギー検出に基づくawkコード。

BEGIN {
        
        n=38
         SelfishCounter=0;
   Normalcounter=0;
}
{
    # Trace line format: energy
        event = $1
        time = $2
        if (event == "r" || event == "d" || event == "s"|| event == "f") {
        node_id = $3
        energy=$14
        }
        if (event=="N"){
        node_id = $5
        energy=$7
        }
    # Store remaining energy
    finalenergy[node_id]=energy
}
END {
    # finalenergy for each node
    
    for (i in finalenergy) {
       finalenergy[i]
        totalenergy = 0
        for (i = 0; i <= n; i = i + 1)  
        totalenergy += finalenergy[i]
    }
    ###compute average energy

    threshold=totalenergy/n
    ####output
    
    for (i=0; i<n; i++) {
        
   if(threshold >finalenergy[i]){

  SelfishCounter++;       }
else if(threshold <finalenergy[i]){
      Normalcounter++;  }
        }
printf "number of selfish nodes:%d \n", SelfishCounter;
        printf "number of normal nodes:%d \n", Normalcounter;
    
       
}

利己的なノードを検出するためのNCRベースのawkコード。.shファイルを使用しました。

BEGIN { 
    ReqCounter=0;
    RepCounter=0;
    NCRCounter=0;
    Normalcounter=0;
    max_node = 0;
} 
    
{action=$1; 
 
if(action == "s" || action == "r" || action == "f") 
{ 
 if(($33 == "REQUEST") && ($5==node))
    ReqCounter++;
if(($33 == "REPLY") && ($5==node))
    RepCounter++;
 
} 
}
END { 
    NCR= ((ReqCounter-(ReqCounter-RepCounter))/ReqCounter)*100;
 

    
 if(NCR <= "30"){
    NCRCounter++; 
    printf "SELFISH\n"; }
else{
  Normalcounter++;
  printf "NORMAL\n";}
    

   # printf "The Node number is:%d \n", node;
   # printf "The NCRis:%d \n", NCR;
    #printf "REQUEST Count:%d \n", ReqCounter;
    #printf "REPLY Count:%d \n", RepCounter;
    
    
    
}

#!/bin/sh
i=0
SelfishNodes=0
NormalNodes=0
while [ $i -lt 38 ]
do
value=$(awk -v node=$i -f NCR.awk DSR.tr)
echo "Node" $i "is" $value
i=`expr $i + 1`
 if [ "$value" = "SELFISH" ]
    then
    SelfishNodes=` expr $SelfishNodes + 1`
 else
    NormalNodes=`expr $NormalNodes + 1`
 fi
 
done
echo "Total Number of Sefiish Nodes are:" $SelfishNodes
echo "Totl Number of Normal Nodes are:"$NormalNodes

上記の2つのawkファイルを一緒に混ぜると

BEGIN {
        
   n=38
   SelfishCounter=0;
   Normalcounter=0;
   ReqCounter=0;
   RepCounter=0;

}
{
    # Trace line format: energy
        event = $1
        time = $2
        if (event == "r" || event == "d" || event == "s"|| event == "f") {
        node_id = $3
        energy=$14
       if(($33 == "REQUEST") && ($5 == node))
        ReqCounter++;
        if(($33 == "REPLY") && ($5 == node))
    RepCounter++;
          }
         if (event=="N"){
        node_id = $5
        energy=$7
        }
       
       
    # Store remaining energy
    finalenergy[node_id]=energy
}
END {
   NCR= ((ReqCounter-(ReqCounter-RepCounter))/ReqCounter)*100;
 
    # finalenergy for each node
    
    for (i in finalenergy) {
       finalenergy[i]
        totalenergy = 0
        for (i = 0; i <= n; i = i + 1)  
        totalenergy += finalenergy[i]
    }
    ###compute average energy

    threshold=totalenergy/n
    ####output
    
 

 if((threshold >finalenergy[i]) || (NCR <= 30)){
    SelfishCounter++; 
    printf "SELFISH\n"; }
else{
  Normalcounter++;
  printf "NORMAL\n";}
    
}
        }
    
       
}
#!/bin/sh
i=0
SelfishNodes=0
NormalNodes=0
while [ $i -lt 38 ]
do
value=$(awk -v node=$i -f energy-ncr.awk DSR.tr)
echo "Node" $i "is" $value
i=`expr $i + 1`
 if [ "$value" = "SELFISH" ]
    then
    SelfishNodes=` expr $SelfishNodes + 1`
 else
    NormalNodes=`expr $NormalNodes + 1`
 fi
 
done
echo "Total Number of Sefiish Nodes are:" $SelfishNodes
echo "Totl Number of Normal Nodes are:"$NormalNodes

次のエラーが発生します

Node 33 is
awk: energy-ncr.awk:59:         }
awk: energy-ncr.awk:59:         ^ syntax error
awk: energy-ncr.awk:62: }
awk: energy-ncr.awk:62: ^ syntax error
Node 34 is
awk: energy-ncr.awk:59:         }
awk: energy-ncr.awk:59:         ^ syntax error
awk: energy-ncr.awk:62: }
awk: energy-ncr.awk:62: ^ syntax error
Node 35 is
awk: energy-ncr.awk:59:         }
awk: energy-ncr.awk:59:         ^ syntax error
awk: energy-ncr.awk:62: }
awk: energy-ncr.awk:62: ^ syntax error
Node 36 is
awk: energy-ncr.awk:59:         }
awk: energy-ncr.awk:59:         ^ syntax error
awk: energy-ncr.awk:62: }
awk: energy-ncr.awk:62: ^ syntax error
Node 37 is
Total Number of Sefiish Nodes are: 0
Totl Number of Normal Nodes are:38

編集者:Ed Morton:以下はOPs 2の最初のawkスクリプトです。次のような形式になっており、gawk -o-コードをより簡単に理解できるようにインデントなどがあります。

スクリプト1:

BEGIN {
        n = 38
        SelfishCounter = 0
        Normalcounter = 0
}

{
        # Trace line format: energy
        event = $1
        time = $2
        if (event == "r" || event == "d" || event == "s" || event == "f") {
                node_id = $3
                energy = $14
        }
        if (event == "N") {
                node_id = $5
                energy = $7
        }
        # Store remaining energy
        finalenergy[node_id] = energy
}

END {
        # finalenergy for each node
        for (i in finalenergy) {
                finalenergy[i]
                totalenergy = 0
                for (i = 0; i <= n; i = i + 1) {
                        totalenergy += finalenergy[i]
                }
        }
        ###compute average energy
        threshold = totalenergy / n
        ####output
        for (i = 0; i < n; i++) {
                if (threshold > finalenergy[i]) {
                        SelfishCounter++
                } else if (threshold < finalenergy[i]) {
                        Normalcounter++
                }
        }
        printf "number of selfish nodes:%d \n", SelfishCounter
        printf "number of normal nodes:%d \n", Normalcounter
}

スクリプト2:

BEGIN {
        ReqCounter = 0
        RepCounter = 0
        NCRCounter = 0
        Normalcounter = 0
        max_node = 0
}

{
        action = $1
        if (action == "s" || action == "r" || action == "f") {
                if (($33 == "REQUEST") && ($5 == node)) {
                        ReqCounter++
                }
                if (($33 == "REPLY") && ($5 == node)) {
                        RepCounter++
                }
        }
}

END {
        NCR = ((ReqCounter - (ReqCounter - RepCounter)) / ReqCounter) * 100
        if (NCR <= "30") {
                NCRCounter++
                printf "SELFISH\n"
        } else {
                Normalcounter++
                printf "NORMAL\n"
        }
        # printf "The Node number is:%d \n", node;
        # printf "The NCRis:%d \n", NCR;
        #printf "REQUEST Count:%d \n", ReqCounter;
        #printf "REPLY Count:%d \n", RepCounter;
}

ベストアンサー1

おすすめ記事