AWK scripts (.awk), which are used to extract the necessary information from trace files (.tr) are also useful to plot graphs using Xgraph (.xgr) utility of NS-2. However, the AWK scripts that are used to plot graphs differ from the AWK scripts that just print the values on the terminal.
I will try to explain the difference between these two types of AWK scripts with the help of an example. Assume that we have designed a tcl script named “energy.tcl” which simulates a network for 100 seconds. We run this tcl script with the following command:
After executing the tcl script we get a trace file (.tr) and a NAM file (.nam) i.e. “energy.tr” and “energy.nam”. Now we may have two scenarios as explained below:
1. We want the value of residual (remaining) energy of a node at the end of the simulation. (Here graph of Time v/s Residual energy is not required).
2. We want the value of residual (remaining) energy of a node at each instance of time i.e from beginning of the simulation to the end of the simulation. (Time v/s Residual energy graph is required to observe the decrease in the total energy of a node).
For both of the above mentioned scenarios we need to design two different AWK scripts. However, the main logic to calculate the residual energy will remain the same. Only the way we print the values differs.
Let scen1.awk be the awk file for first scenario and scen2.awk be the awk file for second scenario. scen1.awk should be designed such that when we run it by using the following command:
awk -f scen1.awk energy.tr
it should print the residual (remaining) energy of a node on the terminal.
And scen2.awk should be designed such that when we run it by using the following command:
awk -f scen2.awk energy.tr > energy.xgr
it should print two values in “energy.xgr”: time and residual (remaining) energy of a node.
If you open “energy.xgr” you will notice that there are two columns created. In first column, values related to “time” are printed and in another column values related to “residual energy” are printed. The values in first column form X-axis of the graph and the values in second column form Y-axis of the graph (thus you get a graph of Time v/s Residual Energy). Give the following command to see the graph:
Default background color of the graph is grey. If you want a graph with background color as white, give the following command:
xgraph energy.xgr -bg white
Other options (like changing background colors, plotting bar graphs, etc) related to Xgraph are available in the pdf file given below:
Hope it helps.
Mohit P. Tahiliani
[Latest Update] You can also check the latest Automated Post Processing (APP) Tool which works based on AWK Scripts and automatically plots the graphs.
Here is the link to APP Tool's blog post: