grep
♥ It is a UNIX command to filter a file.
♥ The name comes from “search globally for lines matching the regular
expression and print them”
♥ grep takes a regular expression on the command line, reads the
standard input or list of files, and outputs the lines containing matches
for the regular expression
In the trace file, if you are interested only in the received packets
In the trace file, if you are intersted only in lines the begin with “s” and have “tcp 1020” later
Executes action commands at the beginning of the script execution,
END { action }
Executes action commands after the end of input.
/pattern/
Prints any lines matching pattern.
{ action }
Executes action for each line in the input.
Example 4
FS : is the Field Separator.
OFS : is the Output Field Separator
Example 6
{ w += NF; c += length}
END { print NR, w, c }
It prints the number of lines, number of words and number of characters in the file.
Example 7
AWK supports associative arrays
BEGIN { FS=”[^azAZ]+”}
{ for (i=1; i<=NF; i++)
words[tolower($i)]++
}
END { for (i in words)
print i, words[i]
}
It gets the frequencies of the words.
Example 8
Functions in AWK
function Square (number, temp) {
temp = number * number
return temp
}
.
.
.
.
print Square(9) # Prints 81
♥ It is a UNIX command to filter a file.
♥ The name comes from “search globally for lines matching the regular
expression and print them”
♥ grep takes a regular expression on the command line, reads the
standard input or list of files, and outputs the lines containing matches
for the regular expression
grep “@” file1.txt > file2.txt
Example 1
In the trace file, if you are intersted only in data concerning tcp packtes that went from node 0 to node 2
grep “0 2 tcp” tr1.tr > tr2.tr
Example 2In the trace file, if you are interested only in the received packets
grep “^r” tr1.tr > tr2.tr
Example 3In the trace file, if you are intersted only in lines the begin with “s” and have “tcp 1020” later
grep “^s” tr1.tr | grep “tcp 1020” > tr2.tr
AWK
♥ AWK is a general purpose computer language
♥ AWK is designed for processing text-based data, either in files or data streams
♥ AWK is designed for processing text-based data, either in files or data streams
♥ The name AWK is derived from the surnames of its authors — Alfred Aho,
Peter Weinberger, and Brian Kernighan;
Peter Weinberger, and Brian Kernighan;
How to run AWK file?
awk -f file1.awk file2.txt
awk -f file1.awk file2.txt > out.txt
awk -f file1.awk file2.txt > out.txt
file1.awk : is a command file
file2.txt : is a primary input file
out.txt : is an output file
BEGIN { action }file2.txt : is a primary input file
out.txt : is an output file
Executes action commands at the beginning of the script execution,
END { action }
Executes action commands after the end of input.
/pattern/
Prints any lines matching pattern.
{ action }
Executes action for each line in the input.
Example 4
{ print $1 }
Example 5
BEGIN { FS = “\t” }
{ nl++ }
{ s=s+$4 }
END { print “average = ” s/nl }
{ nl++ }
{ s=s+$4 }
END { print “average = ” s/nl }
FS : is the Field Separator.
OFS : is the Output Field Separator
Example 6
{ w += NF; c += length}
END { print NR, w, c }
It prints the number of lines, number of words and number of characters in the file.
Example 7
AWK supports associative arrays
BEGIN { FS=”[^azAZ]+”}
{ for (i=1; i<=NF; i++)
words[tolower($i)]++
}
END { for (i in words)
print i, words[i]
}
It gets the frequencies of the words.
Example 8
Functions in AWK
function Square (number, temp) {
temp = number * number
return temp
}
.
.
.
.
print Square(9) # Prints 81
# This program is used to calculate the packet loss rate between nodes 1,2 for the application having the flow id = 2 BEGIN { # Initialization. fsDrops: packets drop. numFs: packets sent fsDrops = 0; numFs = 0; } { action = $1; time = $2; from = $3; to = $4; type = $5; pktsize = $6; flow_id = $8; src = $9; dst = $10; seq_no = $11; packet_id = $12; if (from==1 && to==2 && action == “+”) numFs++; if (flow_id==2 && action == “d”) fsDrops++; } END { printf(“number of packets sent:%d lost:%d\n”, numFs, fsDrops); } |
Example 8:
Standard deviation
BEGIN {FS=”\t”} {ln++} {d=$2-t} {s2=s2=d*d} END {print “standev: ” sqrt (s2/ln)}
/* command to run*/ awk -f average.awk out.tr
Example 9:
Array of calculating average and standard deviation
BEGIN { FS = “\t”} {val[n1]=$4} { n1++ } {s=s+$4} END {
av=s/n1
for (i in val) {
d=val[i]-av
s2=s2+d*d
}
print “average: ” av ” standev ” sqrt(s2/n1)}
av=s/n1
for (i in val) {
d=val[i]-av
s2=s2+d*d
}
print “average: ” av ” standev ” sqrt(s2/n1)}
Example 10
Adding Columns
BEGIN {FS=”\t”}{l1=$2+$3+$4}{l2=$5+$6+$7} \
{print $1″\t” l1″\t” l2″\t”} END {}
{print $1″\t” l1″\t” l2″\t”} END {}
/* command to run*/ awk -f sumcolumn.awk out.tr > outfile
To use tool (i.e awk) to filter only interesting events set tr [open "| awk -f filter.awk >out.tr" w] $ns trace-all $tr .if You are not interested in traces and only results (average, variation, distribution, count etc.) set tr [open "| awk -f my_measure_tool.awk >my_results.txt" w] $ns trace-all $tr
The below is a awk script to analyze several parameters (average e-2-e delay, pdf, normalised routing load and dropped packets,..) for aodv old trace file format. BEGIN { droppedAODVPackets=0; sends=0; recvs=0; # highest packet id can be checked in the trace file for an approximate value highest_packet_id =500000; sum=0; AODV_Adv=0; } { action = $1; time = $2; node_1 = $3; node_2 = $4; src = $5; # For stand alone ad hoc trace files: # if ( packet_id > highest_packet_id ) highest_packet_id = packet_id; # For wired- and cireless trace files. if ($5 =="cbr") { packet_id = $12; } else { packet_id = $6; } #============= CALCULATE DELAY ========================================= # getting start time is not a problem, provided you're not starting # traffic at 0.0. # could test for sending node_1_address or flow_id here. if ( start_time[packet_id] == 0 ) start_time[packet_id] = time; # only useful for small unicast where packet_id doesn't wrap. # checking receive means avoiding recording drops if ( action != "d" ) { if ( action == "r" ) { # could test for receiving node_2_address or flow_id here. end_time[packet_id] = time; } } else { end_time[packet_id] = -1; } #============= CALCULATE PACKET DELIVERY FRACTION============================ # $3 = source node id , here I have 4 source nodes and start my analysis after 490 seconds of simulation (when traffic started) if (( $1 == "s") && ( $7 == "cbr" ) && ( $4 =="AGT" ) && ( ( $3== "_5_" ) || ( $3== "_6_" ) || ($3=="_7_") || ($3=="_8_") )&& ($2 > 490.00 ) ) { sends++;} # $4 = destination node in wired segment of network. if you simulate ad hoc only scenario the change $4 to $3 and %5 to $7. if ( ( $1 == "r") && ( $5 == "cbr" ) && ( $4 == "0" ) && ($2 > 490.00 ) ) { recvs++;} pdf = (recvs/sends)*100; #==================== ADVERTISEMENTS ================== if ( (($1=="f") || ($1=="s") ) && ($4 == "RTR") && ($7 =="AODV" ) && ($2 > 490.00 ) ) { AODV_Adv++; } #============= DROPPED AODV PACKETS ======================================== if ( ($1 == "D") && ($7=="cbr") && ($2 > 490.00 ) ){ droppedAODVBytes=droppedAODVBytes+$8 ; droppedAODVPackets=droppedAODVPackets+1; } } END { for ( packet_id = 0; packet_id <= highest_packet_id; packet_id++ ) { start = start_time[packet_id]; end = end_time[packet_id]; packet_duration = end - start; if ( start < end ) sum= packet_duration+sum; } delay=sum/recvs; printf(" Average e-e delay: \t %f \n", delay); printf(" normalised routing load \t %f \n ", AODV_Adv/recvs); printf("No. of packets sent = %d \n", sends); printf(" No. of packets received = %d \n", recvs); printf(" Pdf (100%) = %f \n \n", pdf); printf("No. of dropped data (packets) = %d \n ",droppedAODVPackets); printf("No. of dropped data (bytes) = %d \n \n ",droppedAODVBytes); printf("No. of aodv advertisements = %f \n ",AODV_Adv); }
No comments:
Post a Comment