awk script automation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script automation
# 1  
Old 10-28-2013
Wrench awk script automation

I have the below code which calculates the time difference between src and dst from a large trace file. The code works for a given source and destination. However, I want to automate the code to go over any src and destination. The format of the source is like that: X.Y where x is always =2 and Y is varying (i.e. 0,1,2,3,4,..). The format of destination: Z.T where Z and T varying (could be 3.5, 5.7, 5.100, .....).
Code:
BEGIN {
  src="2.2431"; dst="5.20"; 
  num_samples = 0;
  total_delay = 0;
}
/^\+/&&$9==src&&$10==dst {
    t_arr[$12] = $2;
};
/^r/&&$9==src&&$10==dst{
    if (t_arr[$12] > 0) {
      num_samples++;
      delay = $2 - t_arr[$12];
    total_delay += delay;
    };
};
END{
  avg_delay = total_delay/num_samples;
  print "Average end-to-end transmission delay is " avg_delay  " seconds";
  print "Measurement details:"; 
  print "  - Since packets are created from the address " src;
  print "  - Until the packets are destroyed at the address " dst;
};

My PROBLEM is how to automate the code and make the condition something like:
/^\+/&&$9==(2.anyNumber)&&$10==(AnyNumber.AnyNumber)

sample of the input file :
Code:
        
         + 0.163944   2    1      a     40  -------       1      2.4      5.4     0    10
        + 0.215400    2    1      a     40  -------       1      2.4      5.4     1    28
        + 0.239528    2    1      t     40  -------        1      2.4      5.4    0    37
        + 0.287784    2    1      t     1040  -------     1      2.4      5.4    1    62
        + 0.287784    2    1      t     1040  -------     1      2.4      5.4    2    63
        - 0.147407    1     0     a     40 -------         1      2.1      5.1    0     6
        r 0.148256     0    5     a     40 -------          1     2.0       5.0    0     2
       + 0.148256     5    0      t     1040 -------      1      5.0      2.0    1     7
       + 0.166969     5    0      t     40 -------         1      5.5      2.5     0     11
        - 0.166969     5    0      t    40 -------          1      5.5      2.5    0    11
        r 0.188072     0    5      a    40 -------          1      2.4      5.4    0    10
        r 0.239528     0    5      a    40 -------          1      2.4      5.4    1    28
        r 0.263656     0    5      t    40 -------          1      2.4      5.4     0    37
        r 0.317128     0    5      t    1040 -------       1      2.4      5.4     1    62
        r 0.318792     0    5      t    1040 -------       1      2.4      5.4     2    63

Any suggestions!!!

Code:
$9 : is the src value (X.Y)
$10: is the dst value(Z.T)

$2 : the time .
$12 is the ID of the packet which unique from src to destination. Since there are middle nodes between src-->destination, the ID might be seen repeated, that's why I taking the average.

the expected output:

Code:
average time from src: 2.Y -->Z.T :   value seconds


Last edited by ENG_MOHD; 10-28-2013 at 01:42 PM.. Reason: Added code tags to data and output
# 2  
Old 10-28-2013
Quote:
Originally Posted by ENG_MOHD
I have the below code which calculates the time difference between src and dst from a large trace file. The code works for a given source and destination. However, I want to automate the code to go over any src and destination. The format of the source is like that: X.Y where x is always =2 and Y is varying (i.e. 0,1,2,3,4,..). The format of destination: Z.T where Z and T varying (could be 3.5, 5.7, 5.100, .....).
Code:
BEGIN {
  src="2.2431"; dst="5.20"; 
  num_samples = 0;
  total_delay = 0;
}
/^\+/&&$9==src&&$10==dst {
    t_arr[$12] = $2;
};
/^r/&&$9==src&&$10==dst{
    if (t_arr[$12] > 0) {
      num_samples++;
      delay = $2 - t_arr[$12];
    total_delay += delay;
    };
};
END{
  avg_delay = total_delay/num_samples;
  print "Average end-to-end transmission delay is " avg_delay  " seconds";
  print "Measurement details:"; 
  print "  - Since packets are created from the address " src;
  print "  - Until the packets are destroyed at the address " dst;
};

My PROBLEM is how to automate the code and make the condition something like:
/^\+/&&$9==(2.anyNumber)&&$10==(AnyNumber.AnyNumber)


Any suggestions!!!



show your data and expected output what is $2 ,$9 ,$10 and $12 ?
# 3  
Old 10-28-2013
Check my update !! Thank for the point
# 4  
Old 10-28-2013
There are some duplicates in your file..

whether you wanted like this ?

Code:
$ cat sample
+ 0.163944 2 1 a 40 ------- 1 2.4 5.4 0 10
+ 0.215400 2 1 a 40 ------- 1 2.4 5.4 1 28
+ 0.239528 2 1 t 40 ------- 1 2.4 5.4 0 37
+ 0.287784 2 1 t 1040 ------- 1 2.4 5.4 1 62
+ 0.287784 2 1 t 1040 ------- 1 2.4 5.4 2 63
r 0.188072 0 5 a 40 ------- 1 2.4 5.4 0 10
r 0.239528 0 5 a 40 ------- 1 2.4 5.4 1 28
r 0.263656 0 5 t 40 ------- 1 2.4 5.4 0 37
r 0.317128 0 5 t 1040 ------- 1 2.4 5.4 1 62
r 0.318792 0 5 t 1040 ------- 1 2.4 5.4 2 63

Code:
$ cat test.sh
#!/bin/bash

if [ -z "$*" ];then echo "No argument exiting.."; exit;fi

awk -v src=$2 -v dst=$3 '
BEGIN {
      num_samples = 0;
      total_delay = 0;
      }

$1 ~ /^\+/ && $9==src && $10==dst {
                        t_arr[$12] = $2;
                       }

$1 ~ /^r/  && $9==src && $10==dst {
                        if (t_arr[$12] > 0) {
                                      num_samples++
                                      delay = $2 - t_arr[$12]
                                    total_delay += delay
                                    }
                          }
END{
  if(num_samples){
            avg_delay = total_delay/num_samples
            print "Average end-to-end transmission delay is " avg_delay  " seconds"
            print "Measurement details:" 
            print "  - Since packets are created from the address " src
            print "  - Until the packets are destroyed at the address " dst
         }
  else
        {
         print "Not Found..."
        }
}' $1

Usage
Code:
$ bash script.sh filename source destination

Code:
$ bash test.sh sample 2.4 5.4
Average end-to-end transmission delay is 0.0265472 seconds
Measurement details:
  - Since packets are created from the address 2.4
  - Until the packets are destroyed at the address 5.4

# 5  
Old 10-28-2013
Actually, the src and dst should be read from the file itself, I don't want to pass the values because there are many like : 2.1, 2.2, 2.3, ......, 2.1489. and the destination has a combination also. the modification is can done to the condition :

$1 ~ /^\+/ && $9==src && $10==dst ..... where the value of the src & dst should be extracted from the input file. I think the way to do probably is the regular expression which I'm newbie to. It should be something like :

/^\+/&&$9==/^2.* && $10== anything.. I will update the above sample

Last edited by vbe; 10-28-2013 at 01:43 PM..
# 6  
Old 10-28-2013
Quote:
Originally Posted by ENG_MOHD
Actually, the src and dst should be read from the file itself, I don't want to pass the values because there are many like : 2.1, 2.2, 2.3, ......, 2.1489. and the destination has a combination also. the modification is can done to the condition :

$1 ~ /^\+/ && $9==src && $10==dst ..... where the value of the src & dst should be extracted from the input file. I think the way to do probably is the regular expression which I'm newbie to. It should be something like :

/^\+/&&$9==/^2.* && $10== anything.. I will update the above sample
do like this then

Code:
$1 ~ /^\+/ && $9~/^2\..*/ && $10

# 7  
Old 10-28-2013
Okay, the condition seems to work ( not sure yet ) but how do I make the code to print the time for each src--->dest. What I'm getting from code I think the last src->dst time. I tried to include the printing before the END block, still getting one value.... I would expect :
Code:
Average end-to-end transmission delay is src 2.y to dst1 --> 0.1 seconds 
Average end-to-end transmission delay is src 2.y to dst2 --> 0.4 seconds
Average end-to-end transmission delay is src 2.y to dst3 --> 0.2 seconds

and so on

Last edited by Scott; 10-28-2013 at 02:55 PM.. Reason: Code tags for data too, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automation Script for Oracle

Hi, As a Oracle Developer, I am writing many Procedures,Functions and Packages. Facing Many optimization issue after writing these Database objects. Trying to tune it manually. Can we write any Shell/Perl/Python script to Optimize these Database objects instead of doing manual check and... (1 Reply)
Discussion started by: vasuvv
1 Replies

2. Shell Programming and Scripting

Automation script

Hello All , I came across a tricky solution to devolop . Here is a part of the requirement automation . I have different set of server say : Web ( has 4 servers under it ) , App ( has 4 servers under it ) , DB ( has 2 servers under it ) Above each i have different load balancers , Say : Web... (4 Replies)
Discussion started by: radha254
4 Replies

3. Shell Programming and Scripting

Script Automation

Hi Gurus, I have a clearcase script that i use to check in a single file at time on my clearcase server. the script is as follows setmyview settask 75098_MSI_TRILOGY_EIM cd /vobs/Trilogy_R12/custom/msieim/12.0.0/sql/ cleartool co -nc . ct mkelem -nc Filename_1.sql cp... (3 Replies)
Discussion started by: r_t_1601
3 Replies

4. Shell Programming and Scripting

Script For Folder Management Automation

Let me start off by saying I am not a linux guy nor do I script much (I modify scripts a bit) but I am looking for a code to help automate file management on usb sticks with clonezilla images. I'm looking for a script to recognize folder names and allow a max of 2 folders with the same text in... (6 Replies)
Discussion started by: traustic
6 Replies

5. Shell Programming and Scripting

sftp automation script not working

Hi all , can any one tell me how to code SFTP automation script ? I searched lots of forums but i didn't get any useful information:( I don't want auto Login script > like if u run the script it should automatically login into specific account and etc.. Just i want the following things... (4 Replies)
Discussion started by: sravan008
4 Replies

6. Shell Programming and Scripting

Help with Shell Script automation

can someone look into this one please... I am struck at this point. I do not know what logic to be followed here. I can go ahead with my work only, if this step is done. Please Help. I have a process X in a shell script. Once the process X is done, it generates a log file. Process X is basically... (1 Reply)
Discussion started by: ss3944
1 Replies

7. Shell Programming and Scripting

FTP automation script

Hi, I have got a requirement like this. a parameterized function custFtp which will take 5 i/ps and will do the following tasks. p1) server name p2) username p3) password p4) path name of the server where the file resides p5) file name pattern the function will work like this. ... (1 Reply)
Discussion started by: ani_datta
1 Replies

8. Filesystems, Disks and Memory

Urgent FTP script automation

Hi guys, Here is my requirement for ftp script that i have to automate in unix using shell script: 1) Find the files that atre created one week from the present day. 2) ftp them to the backup server. 3) At the end of the month make a new directory on my backup server with the new month(eg:Once... (1 Reply)
Discussion started by: koduri0475
1 Replies

9. UNIX Desktop Questions & Answers

Urgent FTP script automation

Hi guys, Here is my requirement for ftp script that i have to automate in unix using shell script: 1) Find the files that atre created one week from the present day. 2) ftp them to the backup server. 3) At the end of the month make a new directory on my backup server with the new month(eg:Once... (1 Reply)
Discussion started by: koduri0475
1 Replies

10. UNIX for Dummies Questions & Answers

Urgent FTP script automation

Hi guys, Here is my requirement for ftp script that i have to automate in unix using shell script: 1) Find the files that atre created one week from the present day. 2) ftp them to the backup server. 3) At the end of the month make a new directory on my backup server with the new month(eg:Once... (1 Reply)
Discussion started by: koduri0475
1 Replies
Login or Register to Ask a Question