Thursday, October 3, 2013

Little_Script_Series: Perl script Count number of Times 4 word on FILE

Ok this is one of the first series on little scripts that i have just used on the work.

Some time ago i was wondering how many users were actually using the mail service either with IMAP or WEBMAIL access so i take parts from the code i found over internet and put it together the perl script needs the name of the file and the word that u want to match the log file that i was working were splitted with blank space so if u need another character just change it!
usage: <loginfile.txt> <place of the word a number>

use strict;
use warnings;

my $num_args = $#ARGV+1;
my $c=0;
my %count_of;
my $user;
my $line;
my @val;
my $k;
my $v;

if($num_args == 0 || $num_args <= 1){
  print ("wrong arguments\n usage: FILE POSITION\n");

  open(myFH,$ARGV[0]) or die("Cant open log file.");
  foreach $line (<myFH>) {
# remove the newline from $line.
    # do line-by-line processing.
    @val = split(/\s+/,$line);

    # select the word by position number.
    $user = $val[$ARGV[1]];
  while ( ($k,$v) = each %count_of ) {
    print "$k => $v\n";

sorry for my bad programming skills but im a newbie, and for the bad english.

No comments:

Post a Comment