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
awk
nawk
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.
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
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.
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
Post a Comment