Search the web
Sign In
New User? Sign Up
perl-beginner · Perl Beginners Mailing List
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Use split   Message List  
Reply | Forward Message #26186 of 26720 |
Re: [PBML] Re: Use split

Hello perl_beg,

> when I compare what is in the file ($datestr =~ @array)

I think your looking for this: ($datestr =~ /@array/)

additionally, some of the logic in your code can condensed.

> my @array = ($day, $mth, $daynum, $year) =
> (split(/
> \s+/,$_))[0,1,2,4];
> if ($datestr =~ @array ) {
> my ($day, $mth, $daynum, $year, $user) =
> (split(/\s+/,$_))
> [0,1,2,4,7];

this doesn't make sense to me. why not just split once?

> $abuser_cnt{$day,$mth,$daynum,$year,$user}++;

I'm not sure what you want to do here...

I believe you could refactor to somdthing along these lines:

while (<INFILE>) {
chomp;
if (m/-j\s?[0-9]?/){
my ($day, $mth, $daynum, $year, $user) =
(split(/\s+/,$_))[0,1,2,4,7];
$abuser_cnt{$user}++ if /$datestr/;
}
}

foreach $user (sort keys %abuser_cnt) {
print "$user\n";
}


Best of luck,
+Dave


--- On Wed, 4/30/08, perl_beg <perl_beg@...> wrote:

> From: perl_beg <perl_beg@...>
> Subject: [PBML] Re: Use split
> To: perl-beginner@yahoogroups.com
> Date: Wednesday, April 30, 2008, 1:17 PM
> Hi David, here is my piece of code. I got the first part
> working
> that you were helping me with originally much thanks.
>
> I tried to include the next part of the code to capture the
> date in
> the logfile. For example - "Tue Apr 20 2008" but
> when I compare what
> is in the file ($datestr =~ @array) of course it's not
> working
> properly. Any suggestions would be great.
>
>
> my $datestr = UnixDate("today","%a %b %e
> %Y");
>
>
> %abuser_cnt = ();
>
> my $file="/var/log/app.log";
>
> open (INFILE, "<$file") || die "Cannot
> open file";
> while (<INFILE>) {
> chomp;
> if ( $_ =~ m/-j\s?[0-9]?/){
> my @array = ($day, $mth, $daynum, $year) =
> (split(/
> \s+/,$_))[0,1,2,4];
> if ($datestr =~ @array ) {
> my ($day, $mth, $daynum, $year, $user) =
> (split(/\s+/,$_))
> [0,1,2,4,7];
> $abuser_cnt{$day,$mth,$daynum,$year,$user}++;
> }
> }
> }
>
> @abusers = sort keys %abuser_cnt;
>
> foreach $user (@abusers) {
> print "$user\n";
> }
>
>
> ------------------------------------
>
> Unsubscribing info is here:
> http://help.yahoo.com/help/us/groups/groups-32.htmlYahoo!
> Groups Links
>
>
>



Fri May 2, 2008 7:48 am

david_v_wright
Offline Offline
Send Email Send Email

Forward
Message #26186 of 26720 |
Expand Messages Author Sort by Date

How could I split the following content on the 8th item (username)? I've tried doing the split(/ /); but was not successful. Thu Apr 3 16:15:11 2008 started...
perl_beg
Offline Send Email
Apr 3, 2008
8:19 pm

... I guess your problem is about variable number of spaces between the months and the day depending on if the day is a single or a double digit. If so, just...
Alexander Saydakov
sandy_saydakov
Offline Send Email
Apr 3, 2008
11:53 pm

Hi Sandy, thanks for the reply which was very helpful. How can I grab just the user name? Would this work? $u_count = split(/\s+/) [7]; ... (username)? ... ...
perl_beg
Offline Send Email
Apr 4, 2008
12:34 pm

... Are you after the substring 'user' in your example string (token #8 with index 7) or after the next token '1' (#9 with index 8)? my $str = 'Thu Apr 3...
Alexander Saydakov
sandy_saydakov
Offline Send Email
Apr 4, 2008
7:11 pm

Hi, You can also use array concept. Below is the scipt. #!/usr/bin/perl print "Hello, World...\n\n\n"; my $str="Thu Apr 3 16:15:11 2008 started 18411 user 1...
PRAVEEN CHAUHAN
best_praveen...
Offline Send Email
Apr 4, 2008
5:54 am

... generally, a perl programmer would prefer something to the affect of: my $str='Thu Apr 3 16:15:11 2008 started 18411 user 1 16:14:25 app=unknown...
david wright
david_v_wright
Offline Send Email
Apr 4, 2008
4:09 pm

Hi Dave, yeah I would say I'm no where near a programmer just do basic scripts. Here is what I'm trying to do. I'm using a hash so I don't get duplicate names...
perl_beg
Offline Send Email
Apr 7, 2008
6:28 pm

Hi Dave, yeah I would say I'm no where near a programmer just do basic scripts. Here is what I'm trying to do. I'm using a hash so I don't get duplicate names...
perl_beg
Offline Send Email
Apr 7, 2008
6:35 pm

Hello perl_beg, I would offer the following suggestions for approaching your task It seems unnecessary in this case to open a pipe to perl open from shell...
david wright
david_v_wright
Offline Send Email
Apr 8, 2008
7:36 pm

Hi Dave, I want to use the open with a while loop to grep certain strings out of this log file because it's massive and those are the only usernames I want....
perl_beg
Offline Send Email
Apr 11, 2008
4:42 pm

... did you try it? ;) my ($col3,$col4,$user) = (split(/\s+/, $str))[2,3,7]; perl is a great language for learning by trying stuff, feel free to experiment. ...
david wright
david_v_wright
Offline Send Email
Apr 13, 2008
9:26 am

Hi Dave, I did try it and did not get the results I wanted. That's because I was missing a few scalar variables ($col3 and $col4). Nice it works fine now. I...
perl_beg
Offline Send Email
Apr 15, 2008
5:14 pm

Hi David, here is my piece of code. I got the first part working that you were helping me with originally much thanks. I tried to include the next part of the...
perl_beg
Offline Send Email
Apr 30, 2008
5:17 pm

Hello perl_beg, ... I think your looking for this: ($datestr =~ /@array/) additionally, some of the logic in your code can condensed. ... this doesn't make...
david wright
david_v_wright
Offline Send Email
May 2, 2008
7:48 am

... perl> open (LOG,"cat /var/log/app.log | egrep ' -j ' | egrep -v ' -j [0- perl> 9]' | grep start|") || die "Cannot open file"; Aha! A "useless use of cat"....
merlyn@...
merlynstoneh...
Offline Send Email
Apr 11, 2008
4:46 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help