As every body suggest, I think regex will help you a lot. Here is short version perlrequick and the complete one perlre.

A few months ago I implemented something similar, here is what I did. I hope it helps for me worked just fine based on my needs. I have included some information to help you understand the process, just in case that something is a bit more complicated just let me know I will try to explain more.

#!/usr/bin/perl use warnings; # it warns about undefined values use strict; # it's a lexical scoped declaration use Data::Dumper; use constant ARGUMENTS => scalar 3; $| = 1; #flushing output my $filename; my $Counting = 0; my $Characters = 0; my $source = $ARGV[0]; my $patern = $ARGV[1]; my $target = $ARGV[2]; my $elements; my $value; my $lines; my @found; my @word; #my $path_source = "/path/".$source.""; #my $path_destination = "/path/".$target.""; open(READ, ,"<", $source) or die "Can not open ".$source.": $!\n"; # < read mode open(WRITE, ,">", $target) or die "Can not open ".$target.": $!\n"; # > write mode if (@ARGV > ARGUMENTS) { print "Too many argument inputs\nCorrect syntax ".$source." ".$pat +ern." name of the file to store the data.\n"; } elsif (@ARGV < ARGUMENTS) { print "Need more argument inputs\nCorrect syntax ".$source." ".$pa +tern." name of the file to store the data.\n"; } else { while ($lines = <READ>) { chomp ($lines); # chomp avoid \n on last field @word = split ((/\s+/), $lines); #\s+ = 1 or more white-space char +acters # \d+ matches one or more integer numbers foreach $value (@word) { if ($value eq $patern) { push (@found, $value); print "I have found one matching pattern: $value\n"; } } } $elements = scalar(@found); print "I found number of occurrences: ".$elements."\n"; print WRITE "I found pattern: $patern, number of occurrences: $ele +ments\n"; } close (READ) or die "READ did not close: $!\n"; close (WRITE) or die "WRITE did not close: $!\n"; $| = 1; #flushing output

You can modified it to meet your needs in sub loop or print error conditions etc.

Seeking for Perl wisdom...on the process...not there...yet!

In reply to Re: Perl script to Parse a file for a string by thanos1983
in thread Perl script to Parse a file for a string by user786

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.