count of word in all lines of file.

any solution to find number of times the word "xyz" in all lines of the file along with line numbers ?
Desire2changeAsked:
Who is Participating?
 
Adam314Commented:

my $count=0;
open(my $IN, "<file.txt") or die "could not open file: $!\n";
while(<$IN>) {
    my $count1=()=/xyz/g;
    $count += $count1;
    print "$.: $count1 times\n" if $count1;
}
close($IN);

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
>> any solution to find number of times the word "xyz"
If you are looking to count the word then you'll need to change the regex suggested by Adam314 from /xyz/ to /\bxyz\b/ to ensure you include word boundries, othrewise you'll count xyz as part of strings rather than as a full word in its own right.

http://www.regular-expressions.info/wordboundaries.html
0
 
Kim RyanIT ConsultantCommented:
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Desire2changeAuthor Commented:
Adham sorry , i am in favour of the code .but instead i clicked NO , can you pls explain this line -

 my $count1=()=/xyz/g;

0
 
ozoCommented:
perldoc perlop
       m/PATTERN/msixpogc
       /PATTERN/msixpogc
               Searches a string for a pattern match, and in scalar context
               returns true if it succeeds, false if it fails.  If no string
               is specified via the "=~" or "!~" operator, the $_ string is
               searched.  (The string specified with "=~" need not be an
               lvalue--it may be the result of an expression evaluation, but
               remember the "=~" binds rather tightly.)  See also perlre.  See
               perllocale for discussion of additional considerations that
               apply when "use locale" is in effect.

               Options are as described in "qr//"; in addition, the following
               match process modifiers are available:

                   g   Match globally, i.e., find all occurrences.
                   c   Do not reset search position on a failed match when /g is in effect.

               If "/" is the delimiter then the initial "m" is optional.  With
...
               The "/g" modifier specifies global pattern matching--that is,
               matching as many times as possible within the string.  How it
               behaves depends on the context.  In list context, it returns a
               list of the substrings matched by any capturing parentheses in
               the regular expression.  If there are no parentheses, it
               returns a list of all the matched strings, as if there were
               parentheses around the whole pattern.

perldoc perldata
       List value constructors

       List assignment in scalar context returns the number of elements
       produced by the expression on the right side of the assignment:

           $x = (($foo,$bar) = (3,2,1));       # set $x to 3, not 2
           $x = (($foo,$bar) = f());           # set $x to f()'s return count

       This is handy when you want to do a list assignment in a Boolean
       context, because most list functions return a null list when finished,
       which when assigned produces a 0, which is interpreted as FALSE.

       It's also the source of a useful idiom for executing a function or
       performing an operation in list context and then counting the number of
       return values, by assigning to an empty list and then using that
       assignment in scalar context. For example, this code:

           $count = () = $string =~ /\d+/g;

       will place into $count the number of digit groups found in $string.
       This happens because the pattern match is in list context (since it is
       being assigned to the empty list), and will therefore return a list of
       all matching parts of the string. The list assignment in scalar context
       will translate that into the number of elements (here, the number of
       times the pattern matched) and assign that to $count. Note that simply
       using

           $count = $string =~ /\d+/g;

       would not have worked, since a pattern match in scalar context will
       only return true or false, rather than a count of matches.
0
 
Desire2changeAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.