kulls has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,
I'm running the perl file in the command prompt and capture the logs in the .log file. say,
perl test.pl > test.log
I want to append the timestamp to the log file before and after the log data. Though test.log is dynamically created,i don't know how do to that.Also i don't want to modify all the scripts,which will return the time stamp along with log data.
Can anyone tell me, how to do this.Is there any commands available in the shell script itself?
Update : How can i get the duration of how much time taken by the 'test.pl' script ?

-kulls

2006-01-30 Retitled by g0n, removed 'OT' marker.
Original title: 'OT Append the timestamp before and after the data in the logfile'

Replies are listed 'Best First'.
Re: Append the timestamp before and after the data in the logfile
by McDarren (Abbot) on Dec 22, 2005 at 05:12 UTC
    Is there any commands available in the shell script itself?
    Yes, there is. It's called the date command.

    This is not how I would choose to do this, but to answer your question directly, you could simply do:

    date > test.log ; perl test.pl >> test.log ; date >> test.log

    Note the use of >> rather than >, this ensures that your logfile is appended to rather than overwritten.

    You should also check man date for the various formatting options.

    Cheers,
    Darren :)

      To save some typing, you can group the commands together in a subshell and redirect the subshell's output instead.

      ( date; perl test.pl; date ) > test.log

          --k.


        No need to spawn a subshell:
        { date; perl test.pl; date; } > test.log
        will do. At least in bash, that is...
      hi,
      Thanx for your reply.I'm proceeding with 'time' command instead of 'date'.
      -kulls
        erm, are you sure this is what you want?

        You said that you wanted a timestamp. The time command will certainly not give you that.

        From man time

        time - run programs and summarize system resource usage

        An example of what you get when you use time is:

        time perl -e 'print "hello world\n"' hello world real 0m0.006s user 0m0.000s sys 0m0.000s

        Unless I seriously misunderstood your original question, then I doubt very much this is what you want.

        Update: It just occurred to me that you're probably running this on Win32, in which case yes, you could use time. You probably want to add the /T switch.

Re: Append the timestamp before and after the data in the logfile
by salva (Canon) on Dec 22, 2005 at 10:02 UTC
    On unix you can use this piece of code to add a timestamp to every line written to the log file:
    select STDOUT; $| = 1; unless (open STDOUT, "|-") { eval { while(<>) { my $now = DateTime->now; print "$now: $_"; } }; exit(0); }
    It uses DateTime to generate the timestamp but changing it to use localtime should be pretty simple.