Re: perl help

2003-03-26 Thread Giorgos Keramidas
On 2003-03-26 14:18, Kenzo <[EMAIL PROTECTED]> wrote:
> I don't know how to write anything in perl and will eventually learn it.
> but I was wondering if anyone would help write a quick perl script for me.
>
> Basically I want the script to look thru a file for certain words and cound
> how many times it finds the word that comes after.
> I have a log file that keeps track of E-mail attachments being send and
> received, and I want to be able to do a count of certain attachments.
> for example. say I see alot of "big this", "big that" and "big nothing"
> I want to be able to see how many times the word that comes after big
> appears in the log file.
> so the output would be like this.
> this5
> that 10
> nothing 20

You don't need Perl for that.  Here's a small trick:

grep 'this' file | wc -l
grep 'that' file | wc -l
...

- Giorgos
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: perl help

2003-03-26 Thread Steve Willoughby
> In the last episode (Mar 27), Giorgos Keramidas said:
> > On 2003-03-26 14:18, Kenzo <[EMAIL PROTECTED]> wrote:
> > You don't need Perl for that.  Here's a small trick:
> > grep 'this' file | wc -l
> > grep 'that' file | wc -l
> Even better:
>   grep -c 'this' file
>   grep -c 'that' file

Unfortunately, that's not what he was asking for, which is to look for 
the pattern "big " where all the possible s are unknown and
report on all the s that were found.

So something like:

while (<>) {
while (/big\s+(\w+)/g) {
$count{$1}++;
}
}

foreach $word (sort(keys(%count))) {
print "$word: $count{$word}\n";
}


ought to do the trick.  Play with $/, etc if you want to allow big and
 to be across a newline from each other.
-- 
Steve Willoughby  | "The purpose of IT is to seamlessly and trans-
Intel DPG Eng. Computing  | parently provide the other nine-tenths of the
Application Development   | iceburg for people who need to work with chunks 
<[EMAIL PROTECTED]>  | of floating ice."   --Strata R. Chalup


___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: perl help

2003-03-27 Thread David Landgren
Kenzo wrote:

[resend. I sent this yesterday but it doesn't appear to have turned up 
on the list.]

I don't know how to write anything in perl and will eventually learn it.
but I was wondering if anyone would help write a quick perl script for 
me.
You really ought to try a different forum. Perl Monks 
(http://www.perlmonks.org/) springs to mind.

Basically I want the script to look thru a file for certain words and 
cound
how many times it finds the word that comes after.
I have a log file that keeps track of E-mail attachments being send and
received, and I want to be able to do a count of certain attachments.
for example. say I see alot of "big this", "big that" and "big nothing"
I want to be able to see how many times the word that comes after big
appears in the log file.
so the output would be like this.
this5
that 10
nothing 20
Well that's pretty easy...

#! /usr/bin/perl -w

use strict;

my $target = shift or die "No target word given on command line";
my $prev;
my %follow;
while( <> ) {
   chomp;
   # break the line into elements
   my @word = split;
   # deal with the leftover of the previous line
   ++$follow{$word[0]} if $prev and $prev eq $target;
   # walk down the line
   $word[$_] eq $target and ++$follow{$word[$_+1]} for 0 .. @word - 2;
   # carry over the last word to the next line
   $prev = $word[-1];
}
# print out the results
print "$_\t$follow{$_}\n" for sort keys %follow;
I hope this is not too confusing.
I might say the same of my script :)

thanks.
You're welcome.
David
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: perl help

2003-03-26 Thread Dan Nelson
In the last episode (Mar 27), Giorgos Keramidas said:
> On 2003-03-26 14:18, Kenzo <[EMAIL PROTECTED]> wrote:
> > Basically I want the script to look thru a file for certain words
> > and cound how many times it finds the word that comes after. I have
> > a log file that keeps track of E-mail attachments being send and
> > received, and I want to be able to do a count of certain
> > attachments. for example. say I see alot of "big this", "big that"
> > and "big nothing" I want to be able to see how many times the word
> > that comes after big appears in the log file.
> >
> > so the output would be like this.
> > this5
> > that 10
> > nothing 20
> 
> You don't need Perl for that.  Here's a small trick:
> 
>   grep 'this' file | wc -l
>   grep 'that' file | wc -l
>   ...

Even better:

grep -c 'this' file
grep -c 'that' file

-- 
Dan Nelson
[EMAIL PROTECTED]
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"