Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

count of word in all lines of file.

Posted on 2008-06-10
6
Medium Priority
?
198 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
6 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 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
Technology Partners: 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 85

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

Industry Leaders: 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!

Question has a verified solution.

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

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.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

916 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