Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK script 1

Status
Not open for further replies.

midou

Programmer
Oct 20, 2002
24
FR
Hi evrybody!
Because I have a simulation trace file that contain informations about packets generated in the network. In order to calculate the transmission delay for every packet I must match the packetId.
I would like to take evry ligne from my inputfile and compare it to all other lignes of my inputfile? Can someone help me?

my pseudo code is like this:

//For every ligne do the following

PacketId = $1; //$1 is the packet Identifier
start = $2; // transmission time
for (i=0; i<=NR; i++)
{ if ($1 == PacketId)
{v //compare with all the imputfile lignes
print $2-start > outputfile
}
}
 
please, post a sample input and the desired output files vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
This the structure of my files:

my input file
PacketID Time node
1 0.2369 1
2 0.2933 1
3 0.3269 1
1 0.3969 2

the desidred outputfile
PacketID Transmission Delay
1 0.1600
2 .....
3 .....
4 .....
 
Hi,Awkscript
Spoke to you over at the C.Net forum.

Awk reads it's input sequentially and once,
though any awk programmer knows at least three
ways around this problem ;)

The easiest thing to do (I am guessing since you don't
post a data sample), is to load the file content into
an array and process it in the end action. Various algos could be used depending on the size of the file.

awk '
function comparison(arrnme,ind1,ind2,tfile, a1, a2) {
split(arrnme[ind1],a1)
split(arrnme[ind2],a2)
if (a1[1] == a2[1] && a1[2] > a2[2]) {
dif = a1[2] - a2[2]
line = sprintf(&quot;%s%s%s%s%d&quot;,a1[1], &quot;is greater than&quot;,
a2[1], &quot;Diff =&quot;, dif
print line >> tfile
close(tfile)
return &quot;1&quot;
}
return &quot;0&quot;
}

{
array[a++] = $1&quot; &quot;$2
}
END {
for (x=0 ; x <= a ; x++) {
for (i=0 ; i <= a ; i++) {
printf &quot;%d\n&quot;,comparison(array,x,i,&quot;myfilename&quot;)
}
}
}

This is not tested. I am going on what you mentioned
at C.Net. Please post sample data so that
others can help you.

 
Thank's marsd!
Is it possible to create a nested loop over the input file lignes? In such a way to do :
for every ligne (corresponding to the sebding time from the first node) of my input file. I search the appropriate ligne (arrival time at the last node) in the input file
the matching is based on the PacketId and node identifier.

Can someone help me?
 
based on my understanding:

nawk -f script.txt inputFile.txt

#--------- inputFile.txt
{
if ( $1 in arrS)
arrE[$1]=$2
else
arrS[$1]=$2
}

END {
for ( i in arrE)
printf(&quot;%d %.4f\n&quot;, i, arrE - arrS);
} vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
ooops - that meant to be a 'script.awk' file of course
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
per-hop latency, eh? interesting.
Again, you need to define what needs to be done clearer.
Pls, post a more detailed sample of input data and the EXACT desired output. vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Awkscript:
For my example you need to invert the comparison
in the comparison function to:

function comparison(arrnme,ind1,ind2,tfile, a1, a2) {
split(arrnme[ind1],a1)
split(arrnme[ind2],a2)
if (a1[1] == a2[1] && a1[2] < a2[2]) {
dif = a2[2] - a1[2]
line = sprintf(&quot;%s%s%s%s%d&quot;,a2[1], &quot;is greater than&quot;,
a1[1], &quot;Diff =&quot;, dif
print line >> tfile
close(tfile)
return &quot;1&quot;
}
return &quot;0&quot;
}


Vlad,
Your solution needs to take into account that each record needs to be compared against all others, if the values match, then an entry needs to be written to a logfile
with the id's(not clear how verbose here, maybe just NR)
and the difference between current record vs compared record.
That's just what I understood from his previous postings
at another site. I could be wrong.

Theres all kinds of ways to do this I just took the brute force approach and some guesswork.
 
my input file is like this: I have for evry packet sent in the network the following informations (arival time of each packet at every network node)
I would like to calculate the delay (between the node0 and the node3) for every packet.

Input
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
time AtNode packetId
0.069 0 1
0.136 1 1
0.145 0 2
0.166 0 3
0.269 2 1
0.299 1 2
0.344 1 3
0.369 3 1
0.411 2 2
0.446 2 3
0.563 3 2
0.693 3 3
+++++++++++++++++++++++++++++++++++++++++++++++++++++++


Output
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PcketId delay
1 0.3
2 0.418
3 0.527
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


my simplified awk program is the following (I know that it wrong):

//For every ligne do the following
if ($2 == 0)
{
PacketId = $3; //$3 is the packet Identifier
start = $1; // transmission time
for (i=0; i<=NR; i++)
{
if ($3 == PacketId)&& ( $2 == 3)
{ //compare with all the imputfile lignes
print PacketId, $2-start >> &quot;output&quot;
}
}
}

I hope that this can help you more! Then yopu can help me!
 
Soprry the AWK programme is like this:

//For every ligne do the following
if ($2 == 0) //At the node 0
{
PacketId = $3; //$3 is the packet Identifier
start = $1; // transmission time
for (i=0; i<=NR; i++)
{
if ($3 == PacketId) && ( $2 == 3)
{ //compare with all the imputfile lignes
print PacketId, $1-start >> &quot;output&quot;
}
}
}
 
unfortunatly this don't work!!!!!!!
:-(
 
Awkscript.
Please get a good awk book and study it.
Arnold Robbins &quot;Effective Awk Programming&quot; is excellent.
Your code as is will never work.

If you would like us to help just post
concise directions like:
&quot;If the 3rd column is the same as any other 3rd column
subtract the second columns and write an entry to the
log file with a description.&quot;
As it is you have confused me so badly I don't know where
to start to help you.
Please do not post any more code, just data samples.
 
something like that should give you a start:

nawk -f late.awk myInput.txt

#------------------------- late.awk
BEGIN {
FLDtime=1
FLDnode=2
FLDpid=3

STARTnode=0
ENDnode=3
}
{
arr[$FLDnode, $FLDpid]=$FLDtime;
if ($FLDpid > maxpid) maxpid=$FLDpid;
}

END {
for (i=1; i<= maxpid; i++)
printf(&quot;%s %.3f\n&quot;,i, arr[ENDnode SUBSEP i] - arr[STARTnode SUBSEP i]);
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
no need for the SUBSEP - sorry:

BEGIN {
FLDtime=1
FLDnode=2
FLDpid=3

STARTnode=0
ENDnode=3
}
{
arr[$FLDnode, $FLDpid]=$FLDtime;
if ($FLDpid > maxpid) maxpid=$FLDpid;
}

END {
for (i=1; i<= maxpid; i++)
printf(&quot;%s %.3f\n&quot;,i, arr[ENDnode, i] - arr[STARTnode, i]);

}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top