Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a tool that outputs to standard output every so often, without loss of generality, let's say that the sequence goes:

mysystem$ ./runme
Starting
Done
mysystem$

Now 'runme' takes a while to complete so I'd really like to get the shell to give me

mysystem$ somecommand ./runme
0952 Starting
1134 Done
mysystem$

Or some approximation of this... any ideas? Would be happy with things like writing to file and so on...

EDIT love the quick response so far but I should have been clearer...

There may well be intermediate statements made by the program so I'd really like to get the shell to give me

mysystem$ somecommand ./runme
0952 Starting
0959 Somewaythough
1011 Mostly done
1134 Done
mysystem$

But this may well be impossible...

share|improve this question
1  
You're asking two completely separate questions. Your edited post requires that you actually have output, while the first one could do without. For example, sleep 5 cannot be timed the way you want it to. Please clarify which it is. – Daniel Beck Nov 10 '11 at 17:18
up vote 2 down vote accepted

Current

$ ./slow
Starting ...
Nearly finished ...
All done.

Desired

$./slow | ./stamp
15:31:19 Starting ...
15:31:29 Nearly finished ...
15:31:31 All done.

Slow prog

$ cat slow
#!/usr/bin/ksh
echo Starting ...
sleep 10
echo Nearly finished ...
sleep 2
echo All done.

Timestamper

$ cat stamp
#!/usr/bin/perl
use strict;
use warnings;
while(<>) {
  my($sec, $min, $hour) = (localtime)[0..2];
  print "$hour:$min:$sec $_";
}

Actually, I'm surprised this works, I thought I'd have to do something about buffering. YMMV

Using sprintf to format the time is left as an exercise for the reader.


For golfers

$ ./slow | perl -n -e 'printf "%02d:%02d:%02d %s", (localtime)[2,1,0],$_'
15:42:17 Starting ...
15:42:27 Nearly finished ...
15:42:29 All done.
share|improve this answer

You can put time in front of the command to execute and it will time the execution.

user@hostname/pwd$ time ls
file1
file2
file3

real      0m0.006s
user      0m0.001s
sys       0m0.003s

Longer commands make for more interesting timings.

share|improve this answer
    
Make this an answer @slhck - this fits best with the OP – Paul Nov 10 '11 at 10:48
    
Didn't the Q ask for "Timestamps" with the hours and minute at which an event occurred? – RedGrittyBrick Nov 10 '11 at 15:23

What about:

ls -la 
total 28
drwxr-xr-x   2 glens glens    27 2011-06-16 09:27 ./
drwxr-xr-x 197 glens glens 69632 2011-11-10 11:17 ../
-rwxrwxrwx   1 glens glens   406 2011-06-16 09:27 SERVER_LOG.txt*

and with sed

ls -la  | sed -e "s/\(.*\)/$(date +%H%M) \1/"
1154 total 28
1154 drwxr-xr-x   2 glens glens    27 2011-06-16 09:27 ./
1154 drwxr-xr-x 197 glens glens 69632 2011-11-10 11:17 ../
1154 -rwxrwxrwx   1 glens glens   406 2011-06-16 09:27 SERVER_LOG.txt*
share|improve this answer

In bash, you can combine a DEBUG trap and PROMPT_COMMAND to print the time directly before and after executing a command.

trap 'echo $( date )' DEBUG
PROMPT_COMMAND='echo $( date )'

This is how it looks like (second output repeated, as PROMPT_COMMAND is a trappable command:

$ sleep 5
Do 10 Nov 2011 18:10:36 CET
Do 10 Nov 2011 18:10:41 CET
Do 10 Nov 2011 18:10:41 CET
share|improve this answer
    
I saw that the user also wants times of intermediate statements, but this might be useful for somebody anyway. – Daniel Beck Nov 10 '11 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .