suggested addition to 'date'

Giorgos Keramidas keramida at ceid.upatras.gr
Sat Aug 12 03:41:18 UTC 2006


On 2006-08-11 20:19, Sam Leffler <sam at errno.com> wrote:
> Julian Elischer wrote:
> > At various times I've wanted to add timestamps to logfiles as they are
> > generated..
> >
> > usually this has involved perl or something to do it.
> 
> pbj% ls | sed -e "s/^/`date +'%+: '`/"
> Fri Aug 11 20:18:05 PDT 2006: Desktop
> Fri Aug 11 20:18:05 PDT 2006: Desktop DB
> Fri Aug 11 20:18:05 PDT 2006: Desktop DF
> Fri Aug 11 20:18:05 PDT 2006: Documents
> Fri Aug 11 20:18:05 PDT 2006: Library
> Fri Aug 11 20:18:05 PDT 2006: Movies
> 	...

Unfortunately this won't work.  The `backquoted` command is expanded
only once -- before the rest of the command-line runs:

$ for sec in 1 2 3 4 5 ; do echo "second $sec" ; sleep 1; done | sed -e "s/^/`date +'%+: '`/"
Sat Aug 12 06:38:03 EEST 2006: second 1
Sat Aug 12 06:38:03 EEST 2006: second 2
Sat Aug 12 06:38:03 EEST 2006: second 3
Sat Aug 12 06:38:03 EEST 2006: second 4
Sat Aug 12 06:38:03 EEST 2006: second 5
$

Using Perl and ``Time::HiRes qw(gettimeofday)'' is the best trick I've
found so far for this sort of thing :-/



More information about the freebsd-current mailing list