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

My problem is i need to get total usage of workstation per day. Only command available to me is the last command which would give something like the following:

root :0 Fri Apr 7 06:00 - 03:00 (08:00)
user :0 Thu Apr 6 03:00 - 23:00 (08:00)
root :0 Thu Apr 6 06:00 - 03:00 (08:00)
root :0 Wed Apr 5 06:00 - 03:00 (08:00)

I dont care about the user just the date. I would like my ouput to look similar to the following:

Apr 7 workstation was in use for 8 hours
Apr 6 workstation was in use for 16 hours
Apr 5 workstation was in use for 8 hours

Something like the ac command (which is not availabe). thanks for any help.

Replies are listed 'Best First'.
Re: total time usage by day with last
by roboticus (Chancellor) on Apr 08, 2006 at 01:34 UTC
    Like this.... ?

    #!/usr/bin/perl -w use warnings; use strict; my (%log); while (<DATA>) { my @fields = split ' '; if ($fields[8] =~ /(\d+):(\d+)/) { my $t = $1 * 60 + $2; $log{"$fields[3] $fields[4]"} += $t; } } for my $day (sort keys %log) { printf "%s workstation was in use for %.1f hours\n", $day, $log{$day}/60.0; } __DATA__ root :0 Fri Apr 7 06:00 - 03:00 (08:00) user :0 Thu Apr 6 03:00 - 23:00 (08:00) root :0 Thu Apr 6 06:00 - 03:00 (08:00) root :0 Wed Apr 5 06:00 - 03:00 (08:00)
    Running it gives me:

    $ ./z.pl Apr 5 workstation was in use for 8.0 hours Apr 6 workstation was in use for 16.0 hours Apr 7 workstation was in use for 8.0 hours
    --roboticus
Re: total time usage by day with last
by GrandFather (Saint) on Apr 08, 2006 at 01:35 UTC

    The general mantra here in reply to this sort of question is: 'What have you tried?'. We like to see at least a little effort before someone asks for a solution to their problem. However, to get you started:

    Read the various Tutorial articles about regular expressions and calling other applications. If you are still in trouble after that, come back to us with some code and ask another question. Read I know what I mean. Why don't you? for some hints on writing good sample code for a question.


    DWIM is Perl's answer to Gödel
Re: total time usage by day with last
by saintmike (Vicar) on Apr 08, 2006 at 06:54 UTC
    Parse the strings, transform them into DateTime objects, create DateTime::Span objects from the usage times and then use the intersection() method on the usage times, passing them a span covering a particular weekday.

    The data format is somewhat ambiguous, though: What does it look like if someone stays logged in for three days? Three years? Is it safe to assume the start/end dates are in the current year?

      That won't work at all. The only useful info is the date and the period of time between brackets. The other time-strings are utter nonsense: how could a terminal have been on between 06:00 and 03:00 on the same day? Did time run backwards?

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        06:00A.M. is logon time and 03:00P.M. logoff time for a total of 8:00hours, but the only useful info is the time between the brackets EX: (8:00). If user is still logged on the message still logged on is returned instead of a total time logged on in brackets. Thats easy enought to get rid of. The problem is when u have someone logged in for (07:42). it is Easy to add 8:00 + 8:00 but when u add 8:00 + 07:42 it doesnt return the correct number because i am dealing with time.

Re: total time usage by day with last
by ikegami (Patriarch) on Apr 08, 2006 at 15:42 UTC

    That data makes no sense to me. People are saying the third time is the time on, but that can't be right. 03:00 to 23:00 is 20 hours. not 8. Furthermore, 06:00 to 03:00 is 9 hours, not 8 (since the 23:00 shows that a 24 hour clock is used).