How to make use of Control Break logic in Shell Programming

fkkaleem
fkkaleem used Ask the Experts™
on
Hi Gurus,

By typing the command $ last, it prints all those users who have last logged into the system.  I would like to break up the repetitive no. of times the user has logged in by using control break and calculate the total time the user was using the system.  I could able to cut the user by applying the command sort and the time from the command last but could not break up the no. of users.   I am illustrating the example for your reference.  Any help is very much appreciated.

Example :

user1    00:20
user1    01:15
user1    00:09
user1    02:03
user2    02:59
user2    00:05
user2    05:33
user2    01:01
user3    02:02
user3    03:00
user3    01:01

The output must appear as :

                      users          Total Time
                      ====          =======
                      user1             03:37
                      user2             08:37
                      user3             06:03

Anticipating a reply soon :))

Thanks in advance gurus :))

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006

Commented:
sort with -u gives unique list !!

but that is not what you want here.....

pipe your data ( as shown above ) to a script

in that script you need to maintain 2 variables

variable1 -- current username
variable2 -- total time logged in ( initialize to 0 )

read data line by line
if username in the line matches current username, then add second field to total time
else output current username and total time and set total time to second field and username to first field

the only problem is the first instance of printing which you may have to handle ... hope you can program it now

Author

Commented:
How to read data line by line?  If u use grep it will search for a particular word but going throughout the file.  Do u mean this?  

How do u pipe the data to a script?  any example??

I have sorted the unique names but how to compare the list of unique names with that of the names from the 'last' command generated.  

Total time logged will require variables for Hours and minutes and not 1 variable and by the way how to set the value comparing it with of the repeated usernames.   Any example will help??

But I dont understand the first instance of printing??


Any example will help me to code it completely.

Thanks in advance,
Regards,
Top Expert 2006
Commented:
suppose you have this list in a file
user1    00:20
user1    01:15
user1    00:09
user1    02:03
user2    02:59
user2    00:05
user2    05:33
user2    01:01
user3    02:02
user3    03:00
user3    01:01

oldusername=
cat <filename> | while read line
do
          username=` echo $line | awk ' { printf $1 } '`
          newtime=` echo $line | awk ' { printf $2 } '`

          if [ $username = $oldusername ]
          then
                       total=`expr $total + $newtime`
          else
                       echo $username $total
                       oldusername=$username
                       total=0
          fi
done

I havent run it.. there might be a couple of syntax errors here but this should give an idea of how to do it

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial