Unix administration - Timestamp filter?

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > July 2006 > Timestamp filter?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Timestamp filter?
Roy Smith

2006-07-02, 1:44 pm

Does anybody know of an easy way to place timestamps on every line of text
in a pipeline as they are produced? For example, if I have a program which
produces:

line 1
line 2
line 3

on stdout, when I run "prog | timestampFilter", I want to get something
like:

Sun Jul 2 10:45:23 EDT 2006: line 1
Sun Jul 2 10:45:50 EDT 2006: line 2
Sun Jul 2 10:46:03 EDT 2006: line 3

I'm not picky about the format of the timestamp; I just want a record of
when each line was produced in some useful way.
ted@loft.tnolan.com (Ted Nolan

2006-07-02, 7:24 pm

In article <roy-84D86C.10484802072006@reader2.panix.com>,
Roy Smith <roy@panix.com> wrote:
>
>
>Does anybody know of an easy way to place timestamps on every line of text
>in a pipeline as they are produced? For example, if I have a program which
>produces:
>
>line 1
>line 2
>line 3
>
>on stdout, when I run "prog | timestampFilter", I want to get something
>like:
>
>Sun Jul 2 10:45:23 EDT 2006: line 1
>Sun Jul 2 10:45:50 EDT 2006: line 2
>Sun Jul 2 10:46:03 EDT 2006: line 3
>
>I'm not picky about the format of the timestamp; I just want a record of
>when each line was produced in some useful way.


How about :


#!/usr/bin/awk -f
#

{
d = strftime();
printf("%s: %s\n",d, $0)
}


(awk is GNU awk in this case)

Ted
Juha Laiho

2006-07-02, 7:24 pm

Roy Smith <roy@panix.com> said:
>Does anybody know of an easy way to place timestamps on every line of text
>in a pipeline as they are produced? For example, if I have a program which
>produces:
>
>line 1
>line 2
>line 3
>
>on stdout, when I run "prog | timestampFilter", I want to get something
>like:
>
>Sun Jul 2 10:45:23 EDT 2006: line 1
>Sun Jul 2 10:45:50 EDT 2006: line 2
>Sun Jul 2 10:46:03 EDT 2006: line 3
>
>I'm not picky about the format of the timestamp; I just want a record of
>when each line was produced in some useful way.


Wrap the program into a shell script, which
- creates a pipe
- forks a background process to read that pipe, and
make the backgroun process run "logger" to read from the pipe
- run the "business" process in foreground, with the message stream
directed to the pipe
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Logan Shaw

2006-07-02, 7:24 pm

Roy Smith wrote:
> Does anybody know of an easy way to place timestamps on every line of text
> in a pipeline as they are produced? For example, if I have a program which
> produces:
>
> line 1
> line 2
> line 3
>
> on stdout, when I run "prog | timestampFilter", I want to get something
> like:
>
> Sun Jul 2 10:45:23 EDT 2006: line 1
> Sun Jul 2 10:45:50 EDT 2006: line 2
> Sun Jul 2 10:46:03 EDT 2006: line 3


#! /bin/sh

while read line
do
echo "`date`: $line"
done

The only problem with this idea is that the filter (no matter what you
implement it in) will probably put the timestamps on as it receives
them, which is not necessarily the same time as when "prog" produces
them, due to buffering.

- Logan
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com