#!/usr/bin/perl -w use strict; $|++; use FileHandle; my $FILE = new FileHandle; # three-argument open, with error handling open($FILE,"<","/var/log/everything") or die "ERROR: can't open file! $!"; # create variables: # $pattern - pattern for regular expression # %parsed - hash, keys are lines containing pattern, # values are counter of times seen # $i - counter of total lines matching pattern my $pattern = 'fwa'; my (%parsed, $i); while(<$FILE>){ { # read line from filehandle, assign to special variable $_ if( /$pattern/i && $i++ ) # search $_ for 'fwa' (case-insensitive) # and increment counter ($i) if found { chomp $_; # remove newline $parsed{ $_ }++; # use line as hash key, increment times seen } } close($FILE); # print output: total entities, and sorted number of each print "\n $i entities\n"; print "$_ x $parsed{$_}\n" for sort keys %parsed;