Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 27 Mar 2003 10:55:19 +0100
From:      David Landgren <david@landgren.net>
To:        freebsd-questions@FreeBSD.ORG
Subject:   Re: perl help
Message-ID:  <3E82CA87.6040804@landgren.net>
In-Reply-To: <DAV44DVRXGsNLqY54z30000b1d0@hotmail.com>
References:  <DAV44DVRXGsNLqY54z30000b1d0@hotmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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.
> this    5
> 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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3E82CA87.6040804>