Solved

count of word in all lines of file.

Posted on 2008-06-10
6
195 Views
Last Modified: 2013-12-25
any solution to find number of times the word "xyz" in all lines of the file along with line numbers ?
0
Comment
Question by:Desire2change
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 21753746

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
 
LVL 40

Expert Comment

by:evilrix
ID: 21755665
>> 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
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 21755818
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Desire2change
ID: 21756642
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
 
LVL 84

Expert Comment

by:ozo
ID: 21756878
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
 

Author Closing Comment

by:Desire2change
ID: 31465861
thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question