Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

4 Answers

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
add comment

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.