Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

count number of lines that matches

count number of lines that matches

count number of lines that matches


I have been reading your newsletters for some time. I would like to know some information on counting the repeating lines in a file. I currently have a script that grabs all URL address to a file, what I am trying to find is count the no. of times the URLs are found in that output file.

Any help will be appreciated.


Below is my script:

use strict;
#use IO::Socket;

open (READ, "sample_scoff.txt") || die " cannot open output file \n";
my @scon_rd = <READ>;
open (WRITE, ">scon_wr.txt") || die " cannot open output file \n";
foreach my $line (@scon_rd) {
    if ($line =~ m/^\d+/m )
        my $num_line = $line;
    #print "$num_line";
        if ($num_line =~ m/\s(http.*?)\s/mg)
        print  "$1 \n\n";
    close (READ);
open (ATon, "scon_wr.txt") || die " cannot open scon write file \n";
my @ATwr = <ATon>;

close (WRITE);
close (ATon);

RE: count number of lines that matches

The easiest (and probably least efficient) way is to read in one file to an array (probably the shortest file) then in a while(<filehandle>) loop on the other file, create a foreach loop on the array and use the $_ eq $arrayelement comparison to check for matches.

Or something along those lines.


RE: count number of lines that matches

Crap.  I didn't read that right... Ignore that last post.  Sheesh, I'm tired.


RE: count number of lines that matches

Okay.  Similar logic to solve the task.  Use multi-dimensional arrays or objects.  On each individual line, create a new instance of the package or element in the array with a sub-element or member variable ($)count.  Use a foreach or for loop to traverse through the array each time and if the URL already exists, increment $count.  If you go the package route, you'll still need to maintain an array with references to each instance.

Three posts later, it's time for bed.

Good luck,


RE: count number of lines that matches

I won't try to write the whole program for you, I don't have the time. But here's a hint. The easiest way to count the number of times some value occurs is to use it as a hash key and increment the value. for example:
Then just print the value for each of the keys in the hash.

RE: count number of lines that matches

I seem to be having a problem typing TGML

here's what it should have looked like:


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close