Solved

count of word in all lines of file.

Posted on 2008-06-10
6
188 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help with powershell script 5 61
BATCH to EXE Converter 2 71
what should i learn? javascript or typescript? 3 38
BatchFile-Disk Detection 23 31
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

730 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