perl help

David Landgren david at
Thu Mar 27 01:21:22 PST 2003

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 
( 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( <> ) {
    # 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.

More information about the freebsd-questions mailing list