AWK Script
AWK is a high level programming language which is used to process text files,named after its three orginal author's name:
A : Alfred Aho
W : Peter Weinberger
K : Brian Kernighan
AWK Scripts are very good in processing the data from the log (trace
files) which we get from NS2. If you want to process the trace file
manually.
AWK PROGRAM STRUCTURE
Awk program structure contains mainly three parts;
1. Begin
2. Content
3. End
BEGIN {<initialization>}
<pattern1> {<actionSet1>}
<pattern2> {<actionSet2>}
. . .
END {<final finalActionSet>}
BEGIN : Begin deals with what
to be executed prior to text file processing,normally which is used to
initialize variable values or constants.
CONTENT : Script which process
the text file. In this part, AWK moves lines by lines (i.e., records by
records) and executes the <actionSet> if the current line match
with the pattern. The actions repeats until AWK reaches the end of the
file.
END : This part explains what
to be executed after the text file processing ie. what to print on the
terminal or to show output in terminal.
EXECUTION
Awk has two types of execution;
1) Plain Execution.
2) Match and Execute.
Plain Execution : Simply AWK statements.
Match and execute : The
second type of execution is “Match and Execute”, when executes plain
execution statements only if the current line (of the input text file)
matches with a predefined pattern. The pattern can be either : 1.
Logical Expression 2. Regular Expression.
Sample Awk Script |
TRACE FILE AND AWK SCRIPT
Here is a sample of trace file from NS2 , to know more about trace file click here
AWK Scripts are very good in processing the data column wise. For
example the first column in the above trace file represents r, s which
indicates receive, sent respectively. If we want to trace the entire r
and s alone from this trace file we can represent it as $1
So
$1 : Action (r,s,d.f)
$2 Time
$3 Time value
$4 node id
$5 node id value
$6 id of next hop
$7 next hop id value
.
.
.
.
More description of trace file is in above link.
To run awk script, we have to use terminal window.
For that first we have to install gawk in Linux.for that we have to write following code in terminal;
sudo apt-get install gawk
after installing gawk, we have to run the awk script using command;
gawk -f <awk file name> <trace file name>
gawk -f file.awk file.tr
The ‘-f’ instructs the awk utility to get the awk program from the source file i.e from .awk file.
After entering command you will get values.
No comments:
Post a Comment