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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

affix timestamp 2

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
Hi,

How can I place the Timestamp at the beginning of each line. Basically creating/adding another column showing the timestamp for each block of data. Note that there are two blocks of data here:

Date 28/06/2006 Time 0:00:40
Date 28/06/2006 Time 0:01:24

And then creating another column showing the average of the three response times. Example output data will be:

Code:
Date 28/06/2006 Time  0:00:40,  1, 98 ms, 99 ms, 100 ms, 172.18.51.98, 99ms

The last data = 99 ms (last column)is the average of the three response time intervals included in the original data.



Code:
---------------------------------
  Date 28/06/2006 Time  0:00:40
---------------------------------
 
Tracing route to [URL unfurl="true"]www.bbc.net.uk[/URL] [212.58.227.74]
over a maximum of 30 hops:
 
  1    98 ms    99 ms   100 ms  172.18.51.98
  2    88 ms    89 ms   100 ms  172.18.12.193
  3   119 ms   110 ms   109 ms  172.26.160.198
  4   118 ms   319 ms   110 ms  172.26.161.234
 
Trace complete.
 
---------------------------------
  Date 28/06/2006 Time  0:01:24
---------------------------------
 
Tracing route to [URL unfurl="true"]www.bbc.net.uk[/URL] [212.58.227.74]
over a maximum of 30 hops:
 
  1   117 ms    98 ms   100 ms  172.18.51.98
  2    88 ms    89 ms    90 ms  172.18.12.193
  3   129 ms   109 ms   130 ms  172.26.160.198
  4   118 ms   109 ms   109 ms  172.26.161.234
  5   128 ms   119 ms   109 ms  172.18.107.226
  
Trace complete.

Many thanks!
 
Try this:

Code:
awk '
BEGIN { OFS="," }
/Date/ { timestamp=$0 }
$1 ~ "[0-9]" {
 printf("%s, %d, %d ms, %d ms, %d ms, %s, %dms\n",
  timestamp, $1, $2, $4, $6, $8, ($2+$4+$6)/3,"ms")
}
' inputfile

Annihilannic.
 
Hi Annihilannic,

I'm not used to this kind of awk format (with BEGIN). Can I put this in one line?

I tried to just copy and paste this in a script file (but of course, I changed the inputfile) and then run it. I got lots of errors.

Please provide more help.
:)
 
Yes, you could put it on one line.

What operating system?

What errors did you get?

Annihilannic.
 
Hi,

This is what I can see from my end:

Code:
zen:$ uname -a
SunOS zen 5.8 Generic_117350-02 sun4u sparc SUNW,Ultra-80
zen:$
zen:$ more test.script
awk 'BEGIN { OFS="," } /Date/ { timestamp=$0 } $1 ~ "[0-9]" {printf("%s, %d, %d ms, %d ms, %d ms, %s, %dms\n",  timestamp, $
1, $2, $4, $6, $8, ($2+$4+$6)/3,"ms")}' file
zen:$
zen:$ sh -x test.script
+ awk BEGIN { OFS="," } /Date/ { timestamp=$0 } $1 ~ "[0-9]" {printf("%s, %d, %d ms, %d ms, %d ms, %s, %dms\n",  timestamp,
$1, $2, $4, $6, $8, ($2+$4+$6)/3,"ms")} file
awk: syntax error near line 1
awk: bailing out near line 1
zen:$

The awk inside test.script is in one continuous line.

Many thanks again :)



 
Try using full paths for awk and file

Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
Hi Mike,

The script didn't work ...

Code:
zen:$ which awk
/bin/awk
zen:$
zen:$ more test.script
/bin/awk 'BEGIN { OFS="," } /Date/ { timestamp=$0 } $1 ~ "[0-9]" {printf("%s, %d, %d ms, %d ms, %d ms, %s, %dms\n",  timesta
mp, $1, $2, $4, $6, $8, ($2+$4+$6)/3,"ms")}' file 
zen:$
zen:$ sh -x test.script
+ /bin/awk BEGIN { OFS="," } /Date/ { timestamp=$0 } $1 ~ "[0-9]" {printf("%s, %d, %d ms, %d ms, %d ms, %s, %dms\n",  timest
amp, $1, $2, $4, $6, $8, ($2+$4+$6)/3,"ms")} file 
awk: syntax error near line 1
awk: bailing out near line 1
zen:$

Thanks, if you have other suggestions ... I'm happy to try it out.

Cheers!
 
try using 'nawk' instead of 'awk'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi vgersh99,

Thanks !!! it did work :) I just replaced awk with nawk...

Code:
zen:$ sh test.script
  Date 28/06/2006 Time  0:00:40, 1, 98 ms, 99 ms, 100 ms, 172.18.51.98, 99ms
  Date 28/06/2006 Time  0:00:40, 2, 88 ms, 89 ms, 100 ms, 172.18.12.193, 92ms
  Date 28/06/2006 Time  0:00:40, 3, 119 ms, 110 ms, 109 ms, 172.26.160.198, 112ms
  Date 28/06/2006 Time  0:00:40, 4, 118 ms, 319 ms, 110 ms, 172.26.161.234, 182ms
  Date 28/06/2006 Time  0:01:24, 1, 117 ms, 98 ms, 100 ms, 172.18.51.98, 105ms
  Date 28/06/2006 Time  0:01:24, 2, 88 ms, 89 ms, 90 ms, 172.18.12.193, 89ms
  Date 28/06/2006 Time  0:01:24, 3, 129 ms, 109 ms, 130 ms, 172.26.160.198, 122ms
  Date 28/06/2006 Time  0:01:24, 4, 118 ms, 109 ms, 109 ms, 172.26.161.234, 112ms
  Date 28/06/2006 Time  0:01:24, 5, 128 ms, 119 ms, 109 ms, 172.18.107.226, 118ms

zen:$

Cheers!
 
Sorry guys but I do need your help again ...
Based from my raw data, how will I get this format?
Each time entry should be in 1 line.

FORMATTED DATA:

Date 28/06/2006 Time 0:00:40,172.18.51.98->172.18.12.193->172.26.160.198->172.26.161.234

Date 28/06/2006 Time 0:01:24,172.18.51.98->172.18.12.193->172.26.160.198->172.26.161.234->172.18.107.226




RAW DATA:
Code:
---------------------------------
  Date 28/06/2006 Time  0:00:40
---------------------------------
 
Tracing route to [URL unfurl="true"]www.bbc.net.uk[/URL] [212.58.227.74]
over a maximum of 30 hops:
 
  1    98 ms    99 ms   100 ms  172.18.51.98
  2    88 ms    89 ms   100 ms  172.18.12.193
  3   119 ms   110 ms   109 ms  172.26.160.198
  4   118 ms   319 ms   110 ms  172.26.161.234
 
Trace complete.
 
---------------------------------
  Date 28/06/2006 Time  0:01:24
---------------------------------
 
Tracing route to [URL unfurl="true"]www.bbc.net.uk[/URL] [212.58.227.74]
over a maximum of 30 hops:
 
  1   117 ms    98 ms   100 ms  172.18.51.98
  2    88 ms    89 ms    90 ms  172.18.12.193
  3   129 ms   109 ms   130 ms  172.26.160.198
  4   118 ms   109 ms   109 ms  172.26.161.234
  5   128 ms   119 ms   109 ms  172.18.107.226
  
Trace complete.
 
Hi Annihilannic,

You mean the expected formatted output I included in my previous post? I only manually typed it here :) based from my past experiences, it's better for me to show the desired script output than explain it in words ...

So basically, its the same principle as the previous one (I mean adding the time stamp in the first column) but then instead of computing for the average of the response times, I'm looking for the actual path where my packet went thru, thus I have this:

TimeStamp,Node_IPAdd1->Node_IPAdd2->Node_IPAdd3->Node_IPAdd4
Date 28/06/2006 Time 0:00:40,172.18.51.98->172.18.12.193->172.26.160.198->172.26.161.234

Hope I explained it clearly thou ....
Thanks in advance :)
 
I understand the requirement. I'm asking whether you have tried to modify the previous solution I provided to give you that output. It's not very difficult.

Annihilannic.
 
I've never tried it yet ... I will try creating my own script but wait as well for any suggestions. I normally ask and then study the suggested script later on. I can learn more stuff when I ask ...

I will wait for the modifications you gonna provide :)
 
viadisky said:
I've never tried it yet ... I will try creating my own script but wait as well for any suggestions. I normally ask and then study the suggested script later on. I can learn more stuff when I ask ...

I will wait for the modifications you gonna provide smile

You can learn even 'more stuff' if you reverse your approach: trying yourself FIRST and asking specific if needed afterwards.

Try it - you'll be pleasantly surprised!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi

I tried it with [tt]gawk[/tt] but should work with other versions too. Most probably with [tt]nawk[/tt].
Code:
awk '$1=="Date"{sub(/^ */,"");s=$0;i=1}$1==i{s=s (i==1?",":"->") $8;i++}$2=="complete."{print s}' /input/file
Note that I did not followed this thread, this is the answer only to your latest question ( 5 Jul 06 10:10 ).

Also note that Annihilannic has right. [tt]awk[/tt] is not so complicated. Like [tt]perl[/tt] for example. ( [tt]man perl[/tt] to see what I am talking about. )

Feherke.
 
This is a bit tricky because I have to group the IP Addresses appearing in one traceroute session and then remove the unnecessary fields (response times) and then retain the Ip Addresses (place them in one line ...)

Hmmm ... okay I'll try :) thanks :)
 
Hi Feherke,

I noticed there's the repeating hypens "---" or the word "Date" signalling another series of traceroutes are listed.

I looked at your script, to be honest I couldn't understand it. I will study yours and Annihilannic's suggested script and see if I can make it to work.

I think I will need a whole night really ... need to study the whole "awk" !!! :-D

Thanks everybody!
 
Hi

viadisky said:
I looked at your script, to be honest I couldn't understand it.
Code:
[blue]$1[/blue]==[green]"Date"[/green] {               [gray]# [u]Date[/u] /06/2006 Time  0:01:24[/gray]
  [red]sub[/red]([teal]/^ */[/teal],[green]""[/green])            [gray]# trim the spaces at the begin of line[/gray]
  [navy]s[/navy]=[blue]$0[/blue]                     [gray]# save the line in a variable[/gray]
  [navy]i[/navy]=[purple]1[/purple]                      [gray]# next trace line will start with 1[/gray]
}
[blue]$1[/blue]==[navy]i[/navy] {                    [gray]# [u]1[/u]  98 ms  99 ms  100 ms  172.18.51.98[/gray]
  [navy]s[/navy]=[navy]s[/navy] ([navy]i[/navy]==[purple]1[/purple]?[green]","[/green]:[green]"->"[/green]) [blue]$8[/blue]   [gray]# add the IP address to the variable[/gray]
[gray]# if is the first IP, then put a comma in front of it, otherwise dash and greater then[/gray]
  [navy]i[/navy]++                      [gray]# increment trace line number[/gray]
}
[blue]$2[/blue]==[green]"complete."[/green] {          [gray]# Trace [u]complete.[/u][/gray]
  [red]print[/red] [navy]s[/navy]                  [gray]# print the variable[/gray]
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top