|
Home > Archive > Unix Shell > February 2006 > sed and timestamp
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]
|
|
|
| Hi,
I have a program that produces line oriented output,
lets say this:
foo x=5 v=14 z=99
foo x=7 v=15 z=101
foo x=7 v=20 z=112
...
and so on, lots of measurement data.
Unfortunately the program does not print an timestamp.
I want to convert it to have an timestamt (YYMMDD-HHMMSS)
060220-143730 x=5 v=14 z=99
060220-143732 x=7 v=15 z=101
060220-143733 x=7 v=20 z=112
....
So I tried this:
fred | sed -e "s/^foo/`date+%y%m%d-%H%M%S`/g"
but unfortunately all lines have the same timestamp
(because the shell calls date only one time at start)
Any idea how I could have the correct timestamp (when the output comes
from the meas. program "fred") ?
Thank you very much,
Frank
| |
| Chris F.A. Johnson 2006-02-20, 5:54 pm |
| On 2006-02-20, Frank wrote:
> Hi,
>
> I have a program that produces line oriented output,
> lets say this:
>
> foo x=5 v=14 z=99
> foo x=7 v=15 z=101
> foo x=7 v=20 z=112
> ..
> and so on, lots of measurement data.
> Unfortunately the program does not print an timestamp.
>
> I want to convert it to have an timestamt (YYMMDD-HHMMSS)
>
> 060220-143730 x=5 v=14 z=99
> 060220-143732 x=7 v=15 z=101
> 060220-143733 x=7 v=20 z=112
> ...
>
>
> So I tried this:
> fred | sed -e "s/^foo/`date+%y%m%d-%H%M%S`/g"
I'd recommend using %Y instead of %y.
> but unfortunately all lines have the same timestamp
> (because the shell calls date only one time at start)
>
> Any idea how I could have the correct timestamp (when the output comes
> from the meas. program "fred") ?
fred | while IFS= read -r foo line
do
timestamp=`date+%Y%m%d-%H%M%S`
printf "%s %s\n" "$timestamp" "$line"
done
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| Bill Marcum 2006-02-20, 5:54 pm |
| On Mon, 20 Feb 2006 15:44:44 +0100, Frank
<fm122@arcor.de> wrote:
> Hi,
>
> I have a program that produces line oriented output,
> lets say this:
>
> foo x=5 v=14 z=99
> foo x=7 v=15 z=101
> foo x=7 v=20 z=112
> ..
> and so on, lots of measurement data.
> Unfortunately the program does not print an timestamp.
>
> I want to convert it to have an timestamt (YYMMDD-HHMMSS)
>
> 060220-143730 x=5 v=14 z=99
> 060220-143732 x=7 v=15 z=101
> 060220-143733 x=7 v=20 z=112
> ...
>
>
> So I tried this:
> fred | sed -e "s/^foo/`date+%y%m%d-%H%M%S`/g"
>
> but unfortunately all lines have the same timestamp
> (because the shell calls date only one time at start)
>
> Any idea how I could have the correct timestamp (when the output comes
> from the meas. program "fred") ?
>
fred | while read a b; do
date "+%y%m%d-%H%M%S $b"
done
--
Live within your income, even if you have to borrow to do so.
-- Josh Billings
Why won't you let me kiss you goodnight? Is it something I said?
-- Tom Ryan
| |
| Icarus Sparry 2006-02-20, 5:54 pm |
| On Mon, 20 Feb 2006 15:44:44 +0100, Frank wrote:
> Hi,
>
> I have a program that produces line oriented output,
> lets say this:
>
> foo x=5 v=14 z=99
> foo x=7 v=15 z=101
> foo x=7 v=20 z=112
> ..
> and so on, lots of measurement data.
> Unfortunately the program does not print an timestamp.
>
> I want to convert it to have an timestamt (YYMMDD-HHMMSS)
>
> 060220-143730 x=5 v=14 z=99
> 060220-143732 x=7 v=15 z=101
> 060220-143733 x=7 v=20 z=112
> ...
>
>
> So I tried this:
> fred | sed -e "s/^foo/`date+%y%m%d-%H%M%S`/g"
>
> but unfortunately all lines have the same timestamp
> (because the shell calls date only one time at start)
>
> Any idea how I could have the correct timestamp (when the output comes
> from the meas. program "fred") ?
>
> Thank you very much,
> Frank
Since you said 'lots of measurement data', in addition to the other
suggestions, you might consider the following.
#!/usr/bin/expect
# Small program to put a timestamp onto each line
proc now {} { clock format [clock seconds] -format "%Y%m%d-%H%M%S" }
eval spawn -noecho $argv
log_user 0
set timeout -1
expect -re "\[^\n]*\n" { puts -nonewline "[now] $expect_out(buffer)" ;
exp_continue } eof {}
(This example program assumes that you don't need to interact with the
program). Put this script into a fime (say "timestamp"), and then run
expect timestamp fred
There are two advantages over suggestions where you pipe into a while
loop. Firstly this does not use a process for each line of output. Unix
processes are cheap, but not that cheap. Secondly the output of the
command is not buffered. If your program produces data say once per
second, is using the stdio library, and is not taking steps to flush the
buffer after every line, then setting up a pipe will typically cause it to
buffer 4k of output. Your example lines are about 20 characters long, so
the buffer will hold about 200 of them. The while loop suggestions will
cause these 200 lines all to have about the same timestamp, rather than
spread over 3 minutes.
You can use 'sed' to remove the "foo" if you wish,
expect timestamp fred | sed 's/^\([0-9]* \)foo/\1/'
The disadvantage is that you need to have the "expect" program installed.
|
|
|
|
|