Skip to main content

AWK Programming for Packet Analysis | NS2 Basics | Lecture 12

AWK Programming

In this post, you will learn

  • How to create an awk script

  • How to analyse a given trace file using AWK scripts

Introduction

AWK is a text processing programming language developed at AT&T by A V Aho, P J Weinberger and B W Kernighan.  The first letters of these people make the script AWK (Aho, Weinberger, Kernighan)

There are three different versions of AWK 

  1. awk

  2. nawk

  3. gawk

If the operating system is Linux, then it would be awk or gawk (GNU awk). This chapter tells you the very basic things about awk, and for elaborate help, refer [6]

Awk is a pattern-directed scanning and processing language. The column-wise processing is powerful as the prediction of results, analysis of data, etc are possible using this method.  

Syntax: 

awk [-Ffs] [-v var=value] [ -f file ...] [file ...]

Options: 

-Ffs option is  -F, and  fs  is field separator

-f specify an awk program

There are three different ways to execute an awk program:

Method 1:

awk '{print $1}' infile > outfile

Method 2:

awk -f awkprog   infile > outfile

Here, the awkprog is a separate file that contains

{

print $1 

}

Method 3

awkprog infile outfile 

The contents of the awkprog file as given below (it is also an executable file)          

awk '{

print $1

}' $1 >$2

Syntactically, a rule consists of a pattern followed by an action. The action is enclosed in curly braces to separate it from the pattern. Newlines usually separate rules. Therefore, an awk program looks like this

pattern { action }

pattern { action }

….

Missing action means print the line that matched the pattern.

e.g  /abc/   -print all the lines from a file where the abc string is found

missing pattern means an action to be done for all the lines

e.g { print $2 }   -print the second column for all the records

Arithmetic Operators

Similar to C programming, there are several arithmetic operators available in awk. 

Operator

Type

Meaning

Expression

Result

+

Arithmetic

Addition

9+2

11

-

Arithmetic

Subtraction

9-2

7

*

Arithmetic

Multiplication

9*2

18

/

Arithmetic

Division

9/2

4.5

%

Arithmetic

Modulo 

9%2

1

<space>

String

Concatenation

9 2

92

The – and + can also be operated as an unary operator. If x is 4 then, 

print –x  will display -4 


Commands in AWK

There are only a limited number of commands in awk. They also form statements. Some of them are 

  • if (expression) statement [else statement]

    • if (condition)  statement1 else statement2

  • while (expression) statement

    • while(condition)   statement

  • for (expression;expression;expression) statement

    • for (exp1 ; condition ;exp2 ) statement

  • for (variable in array) statement

  • do statement while (expression)

    • break  (** break -- to exit from a "while" or "for" loop)

    • continue

  • {[statement ...]}

  • var=expression

  • print[expression-list] [>expression]

  • printf format [,expression-list ] [>expression]

  • return [expression]

  • next

  • delete array [expression]

  • exit [expression]


Built-in Variables in AWK

  • FS   -field separator

  • OFS  -output field separator

  • NF   -no of fields 

  • NR   -number record 

  • FNR  -file record number

  • RS   -record separator

  • ORS  -output record separator

  • ARGC -argument count

  • ARGV -argument variables

  • FILENAME -current file name


Escape characters:

  • \a -  alert character

  • \b - backspace character

  • \f - form-feed character

  • \n - new-line character

  • \r - carriage-return character

  • \t - tab character

  • \v - vertical-tab character

Simple examples on Awk

Listing B.1 – AWK Script to print the data file as it is.

Input Data 

1, Pradeep,1000

2,kumar,2000

3, Krishna,3000


AWK Script 


{

print

}


Output Data:

1, Pradeep,1000

2,kumar,2000

3, Krishna,3000


Listing B.2 – AWK Script to print only the first column

Input Data

1, Pradeep,1000

2,kumar,2000

3, Krishna,3000

AWK Script 

BEGIN { FS="," }

{

  print $3 |" sort > out.dat "

}

Output Data: (You need to open the file out.dat)

1

2

3

4  


If $3 was used, then the output will be changed to 

1000

2000

3000


Listing B.3: Print the duplicate records

Input Data

1

2

3

5

5

6

6

7

8

9

9

AWK Script

  

{

    if (NR==1) {no=$1;i=0;next}

    if (no==$1) print "Duplicate Entry:\t "no

     else  

     no=$1

}


Output Data:

Duplicate Entry:5

Duplicate Entry:6

Duplicate Entry:9


Computing the Performance parameters using AWK

AWK scripts are much more powerful in analysing the network performance, particularly when logs or trace files are generated. Usually in networks, trace files are generated irrespective of whether the device is a simulator, emulator or even in an experimental setup. In NS2, each simulation generates an animation file and a trace file. The trace files can be processed using awk scripts to measure the following performance metrics like 

  • Throughout the network

  • Packet loss and packet drop calculation

  • Energy calculation

  • Packet delivery ratio

  • End-to-end delay.

  • Jitter 

  • Plotting the Congestion window

Here is the input data for the following scripts 

Input Data:

r 3.885894108 _0_ AGT  --- 152 ack 60 [13a 0 1 800] ------- [1:0 0:0 32 0] [68 0] 1 0

s 3.885894108 _0_ AGT  --- 163 tcp 1040 [0 0 0 0] ------- [0:0 1:0 32 0] [88 0] 0 0

r 3.885894108 _0_ RTR  --- 163 tcp 1040 [0 0 0 0] ------- [0:0 1:0 32 0] [88 0] 0 0

s 3.885894108 _0_ RTR  --- 163 tcp 1060 [0 0 0 0] ------- [0:0 1:0 32 1] [88 0] 0 0

r 3.886183181 _1_ MAC  --- 0 ACK 38 [0 1 0 0] 

s 3.886533108 _0_ MAC  --- 0 RTS 44 [256e 1 0 0] 

r 3.886885181 _1_ MAC  --- 0 RTS 44 [256e 1 0 0] 

s 3.886895181 _1_ MAC  --- 0 CTS 38 [2434 0 0 0] 

r 3.887199254 _0_ MAC  --- 0 CTS 38 [2434 0 0 0] 

s 3.887209254 _0_ MAC  --- 130 tcp 1118 [13a 1 0 800] ------- [0:0 1:0 32 1] [73 0] 0 0

r 3.896153327 _1_ MAC  --- 130 tcp 1060 [13a 1 0 800] ------- [0:0 1:0 32 1] [73 0] 1 0

s 3.896163327 _1_ MAC  --- 0 ACK 38 [0 0 0 0] 

r 3.896178327 _1_ AGT  --- 130 tcp 1060 [13a 1 0 800] ------- [0:0 1:0 32 1] [73 0] 1 0

Here is the list of awk scripts that were helpful in measuring the above metrics. 

Listing B.4 – Throughput 


AWK Script


BEGIN {

       recvdSize = 0

       startTime = 400

       stopTime = 0

  }

   

  {

             event = $1

             time = $2

             node_id = $3

             pkt_size = $8

             level = $4

   

  # Store start time

  if (level == "AGT" && event == "s" && pkt_size >= 512) {

    if (time < startTime) {

             startTime = time

             }

       }

   

  # Update total received packets' size and store packets arrival time

  if (level == "AGT" && event == "r" && pkt_size >= 512) {

       if (time > stopTime) {

             stopTime = time

             }

       # Rip off the header

       hdr_size = pkt_size % 512

       pkt_size -= hdr_size

       # Store received packet's size

       recvdSize += pkt_size

       }

  }

   

  END {

       printf("Average Throughput[kbps] = %.2f\t\t StartTime=%.2f\tStopTime=%.2f\n",(recvdSize/(stopTime-startTime))*(8/1000),startTime,stopTime)

  }


Output:

The output shows the throughput along with the StartTime and StopTime of the simulation


Listing B.5 – Packet Delivery Ratio

AWK Scripts


BEGIN {

        sendLine = 0;

        recvLine = 0;

        fowardLine = 0;

}


$0 ~/^s.* AGT/ {

        sendLine ++ ;

}

$0 ~/^r.* AGT/ {

        recvLine ++ ;

}

$0 ~/^f.* RTR/ {

        fowardLine ++ ;

}

END {

        printf "cbr s:%d r:%d, r/s Ratio:%.4f, f:%d \n", sendLine, recvLine, (recvLine/sendLine),fowardLine;

}

Output:



Listing B.6 – Script for calculating Send,Received and Dropped packets and also Delivery Ratio, Average End-to-End Delay.


AWK Script 

BEGIN {

seqno = -1; 

droppedPackets = 0;

receivedPackets = 0;

count = 0;

}

{

#packet delivery ratio

if($4 == "AGT" && $1 == "s" && seqno < $6) {

seqno = $6;

} else if(($4 == "AGT") && ($1 == "r")) {

receivedPackets++;

} else if ($1 == "D" && $7 == "tcp" && $8 > 512){

droppedPackets++; 

}

#end-to-end delay

if($4 == "AGT" && $1 == "s") {

start_time[$6] = $2;

} else if(($7 == "tcp") && ($1 == "r")) {

end_time[$6] = $2;

} else if($1 == "D" && $7 == "tcp") {

end_time[$6] = -1;

}

}


END { 

for(i=0; i<=seqno; i++) {

if(end_time[i] > 0) {

delay[i] = end_time[i] - start_time[i];

count++;

}

else

{

delay[i] = -1;

}

}

for(i=0; i<count; i++) {

if(delay[i] > 0) {

n_to_n_delay = n_to_n_delay + delay[i];

}

n_to_n_delay = n_to_n_delay/count;

print "\n";

print "GeneratedPackets = " seqno+1;

print "ReceivedPackets = " receivedPackets;

print "Packet Delivery Ratio = " receivedPackets/(seqno+1)*100

"%";

print "Total Dropped Packets = " droppedPackets;

print "Average End-to-End Delay = " n_to_n_delay * 1000 " ms";

print "\n";

}


Output:


Conclusion:

AWK scripts is a powerful tool helpful in processing the trace files generated by the network elements. AWK processes the scripts based on pattern matching and helpful in plotting the network performance characteristics like throughput, packet delivery ratio, end to end delay, packet loss, drop calculation and other factors.


Comments

Popular posts from this blog

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libc...

Simulation of URDF, Gazebo and Rviz | ROS Noetic Tutorial 8

Design a User-defined robot of your choice (or you can use the URDF file) and enable the LIDAR Scanner so that any obstacle placed on the path of the light scan will cut the light rays. Visualize the robot in the Gazebo workspace, and also show the demonstration in RViz.   (NB: Gain knowledge on wiring URDF file and .launch file for enabling any user-defined robot to get launched in the gazebo platform.) SLAM : One of the most popular applications of ROS is SLAM(Simultaneous Localization and Mapping). The objective of the SLAM in mobile robotics is to construct and update the map of an unexplored environment with the help of the available sensors attached to the robot which will be used for exploring. URDF: Unified Robotics Description Format, URDF, is an XML specification used in academia and industry to model multibody systems such as robotic manipulator arms for manufacturing assembly lines and animatronic robots for amusement parks. URDF is especially popular with users of the ...

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb http://in.archive.ubuntu.com/ubuntu/ bionic main universe $ sudo apt update Since, it...