合并两个 awk 文件

合并两个 awk 文件

我正在尝试根据 ncr 和能量方程来检测自私节点。我已经检测了自私节点,但分别基于 ncr 和能量。并且我在结合这两个条件时遇到了问题(如果节点的 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

当我将上面显示的两个 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;
}

相关内容