Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search, count the occurence, and write total of a string in text file

Status
Not open for further replies.

sappleg

Programmer
Apr 15, 2008
31
CA
Hi,

I want to search for a string in a text file and print the summary of total count of how many of it exists in the file.

For example, here my text in the file from which I want to count how many times 'Status: Updated' and 'Status: Created' occured each. Item UPC: 111, Desc: ALMOST, Price: 0, Status: Updated


Item UPC: 112, Desc: CRAZY BALL, Price: 0, Status: Updated
Item UPC: 112, Desc: DVD, Price: 0, Status: Updated
Item UPC: 113, Desc: dvd, Price: 0, Status: Updated

Item UPC: 114, Desc: CARS, Price: 8.25, Status: Created
Item UPC: 115, Desc: CARS RACE, Price: 0.01, Status: Created
Item UPC: 116, Desc: BOXES, Price: 49.99, Status: Created

So, the result/summary to be printed as:

Total Status: Updated = 4, Status: Created = 3

Any way to do this please?

Thanks in advance.
This is what I tried, but getting compilation errors. So, not working. Any alternative method to achieve this please:

Code:
open my $fh, '>', 'Product_Import_Log.txt');
 my %count;
 while (<$fh>)
   {
    if (/Status:\s+(\w+)/)
       {
        $count{$1}++;
       }
   }
 print 'Totals: ', (\%count);
 
sure your getting errors.. your missing the opening (, try writing with something like notepad++ that does color coding and bracket matching.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top