Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
here's some well-behaved code that should do what you want. notice use strict;, -w, and error checking. notice the use of a hash (anytime you think unique, think hash.) also note, close filehandles when you're done with them.

by the way, i've tested this, and it works for me.

oh, and you should use the reply link to the right of the post, to make sure the author you're replying to sees the response.

#!/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;

~Particle


In reply to Re: File Search And Compare by particle
in thread File Search And Compare by PyroX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-25 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found