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!

checkLogin.pl
usage: checkLotin.pl <loginfile.txt> <place of the word a number>

#!/usr/bin/perl
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: checkLogin.pl FILE POSITION\n");
}

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

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


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

No comments:

Post a Comment