Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to match the each line of of users for a perticular time with predeclared group.

by perladdict (Chaplain)
on Jan 18, 2007 at 16:41 UTC ( #595282=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have one file which contains the information of all the
users who r login to the machine in between 10 minutes,the
file contains fallowing format
time maxusrat_a_time loggeduser usernames
1.00 50 10 x,y,z,w,u,e,g,j,k,a
1.10 50 13 x,y,b,a,g,j,k,r,,n,m,s,
1.20 50 05 c,t,g,q,f
1.50 50 08 a,t,y,w,z,x,s,b
2.00 50 14 x,y,u,b,g,f,s,a,i,o,p,c
from the above file i grouped the usernames in to 3 groups
so,that i want to calculate for a perticilar intervel how meny of group1 ,group2 and group3 are loggedin to the server.

i need result like below 1.00 5(group1)user,5(group2)users are logged in


fallowing is the code i am trying to solve the solution
@GRP1=(ravindkr,anandku,mparamas,anbarasr,subashin,prakasku,bsendray, +pregunat,andccadm,nkhuntia,vdeshpan,dbalasub,rbalakri); @GRP2=(awsdel,lpalanic,masubram,bpakanat,mshifan,asrajama,vidhyan,apat +nayi,samallik,ikrishna,adityam,susajja,vramana,raghuran); @GRP3=(mahendrd,ranantan,knittala,sathyam,bmallapr,skummara,prasantn,s +rivatss,ramack,dmurugap,pparichi,esettu,babuk,hpandura,brengana); @GRP4=(thendraa,samarenb,slekkala,apatnayi,psundar,isivapra,pjayaram,s +unandap,speriasa,raallaga,gelango,dchockal,ldash); open(FH,"_today_data.txt") or die "can't open the file$!\n"; while($line=<FH>) { chomp($line); @usr=substr($line,8,2); @time=substr($line,0,4); @username=substr($line,16); foreach $a (@username) { @username1 = split(',', $a); print "@time\t @usr\t @username1\n"; } }
keep pouring your ideas.......

Comment on How to match the each line of of users for a perticular time with predeclared group.
Download Code
Re: How to match the each line of of users for a perticular time with predeclared group.
by wfsp (Abbot) on Jan 18, 2007 at 18:07 UTC
    Couple of points.

    If you want to lookup each users group then one way is to build a lookup table (a hash).

    I would print the report after all the data has been parsed building an output hash as you go.

    I've used the user names you used in your sample data to keep things simple.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %groups; $groups{'g1'} = ['a'..'g']; $groups{'g2'} = ['h'..'n']; $groups{'g3'} = ['o'..'u']; $groups{'g4'} = ['v'..'z']; my %lookup; for my $grp (keys %groups){ %{$lookup{$grp}} = map {$_ => undef} @{$groups{$grp}}; } my %output; while(my $line=<DATA>){ chomp($line); my $usr=substr($line,8,2); my $time=substr($line,0,4); my $username_string = substr($line,11); my @usernames= split ',', $username_string; for my $name (@usernames){ for my $grp (keys %lookup){ $output{$time}{$grp}++ if exists $lookup{$grp}{$name}; } } } for my $time (sort keys %output){ print "$time\t"; for my $grp (sort keys %groups){ my $logged_in = 0; $logged_in = $output{$time}{$grp} if exists $output{$time}{$grp}; print "$logged_in ($grp) users\t"; } print "are logged in\n"; } __DATA__ 1.00 50 10 x,y,z,w,u,e,g,j,k,a 1.10 50 13 x,y,b,a,g,j,k,r,,n,m,s, 1.20 50 05 c,t,g,q,f 1.50 50 08 a,t,y,w,z,x,s,b 2.00 50 14 x,y,u,b,g,f,s,a,i,o,p,c
    output:
    ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" monk.pl 1.00 3 (g1) 2 (g2) 1 (g3) 4 (g4) users logged in 1.10 3 (g1) 4 (g2) 2 (g3) 2 (g4) users logged in 1.20 3 (g1) 0 (g2) 2 (g3) 0 (g4) users logged in 1.50 2 (g1) 0 (g2) 2 (g3) 4 (g4) users logged in 2.00 5 (g1) 1 (g2) 4 (g3) 2 (g4) users logged in > Terminated with exit code 0.
    updated: tidied up the output.

      If the group assignments are mutually exclusive (no person is assigned to more than one group ... which doesn't look to be the case, as 'apatnayi' is in groups 2 and 4), rather than loop through each of the groups, it's easier to have the lookup be a mapping of the group assignments:

      my %lookup = map { my $grp = $_; map { $_ => $grp } ( @{ $groups{$grp} }; } (keys %groups); # ... and then below ... foreach my $name (@usernames) { $output{$time}{ $lookup{$name} }++; # ... or, if you have users logged in that aren't in a group # $output{$time}{ $lookup{$name} }++ if exists $lookup{$name}; # ... or, to track those users with no groups # $output{$time}{ $lookup{$name} || 'unknown' }++; }

      If it's not, then you can have the lookup be to an array of what groups they're in, so you don't have to look through each group every time:

      my %lookup = (); foreach my $grp (keys %groups) { push @{$lookup{$_}}, $grp foreach @{$groups{$grp}}; } ... foreach my $name (@usernames) { $output($time){$_}++ foreach @{$lookup{$name}}; }
Re: How to match the each line of of users for a perticular time with predeclared group.
by liverpole (Monsignor) on Jan 19, 2007 at 01:57 UTC
        keep pouring your ideas.......

    That's a very interesting signature.  How did you happen to choose it?


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      keep pouring your ideas.......

      That's a very interesting signature. How did you happen to choose it?

      Sounds familiar indeed, ++! A good .sig as well... although I'd amend to "keep pouring your ideas... with moderation"!

      That was jesuashok's .sig before his katharsis...

      --shmem

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://595282]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-12-19 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (70 votes), past polls