Awk programming is very easy to learn and simple to apply. Awk in NS2 takes the trace file for analysis. $1, $2, $3 etc are used to access column values from the trace file.
Generally an awk program will have a structure as shown below:
BEGIN {<initialization>}
<pattern 1> {<actions>}
<pattern 2> {<actions>}
.
.
.
<pattern N> {<actions>}
END {<final actions>}
Execution of AWK script:
awk -f filename.awk tracefile.tr
Given below is the code for energy analysis using awk script.
BEGIN {
seqno = -1;
start_time = 0;
}
{
action = $1;
time = $2;
node_id = $3;
layer = $4;
flags = $5;
seqno = $6;
type = $7;
size = $8;
a = $9;
b = $10;
c = $11;
d = $12;
energy = $14;
if(seqno < $6 && node_id == "_4_" && (action == "r" || action == "s")) {
seqno = $6;
remaining_energy = energy;
start_time = time;
printf("%f\t%f\n",start_time,remaining_energy);
}
}
END {
}