CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
script open and count files

 



tkrason
New User

Nov 23, 2013, 8:08 AM

Post #1 of 6 (1239 views)
script open and count files Can't Post

Hi all.
need to write a script that will open only txt files in the specified directory and count lines containing the word TEST. Ask you for help and many thanks in advance.


Kenosis
User

Nov 23, 2013, 8:29 AM

Post #2 of 6 (1237 views)
Re: [tkrason] script open and count files [In reply to] Can't Post

What have you tried?


tkrason
New User

Nov 23, 2013, 8:38 AM

Post #3 of 6 (1232 views)
Re: [Kenosis] script open and count files [In reply to] Can't Post

this script count txt files


use strict;
use warnings;
use File::Find;

my $dir = 'C:\Perl64';
my $count;

my $szukaj = sub{

$count++ if $File::Find::name =~ /\.txt$/
};

find($szukaj, $dir);

print $count, "\n";



this open file and show lines contains word TEST


open(FILE, 'c:\perl64\Artistic.txt')
or die "Could not open file 'ppp' $!";
$string = "TEST";
while (<FILE>) {
chomp $row;
if (/$string/) {print}
}


but i don't know what join this scripts


BillKSmith
Veteran

Nov 23, 2013, 8:51 AM

Post #4 of 6 (1228 views)
Re: [tkrason] script open and count files [In reply to] Can't Post

Hint: Use the function 'glob' (refer perldoc -f glob) to select the text files. For each file, open the file, and read it line-by-line. Increment the count for each line that matches your string. Close the file. When you are done with all the files, print the count.
Good Luck,
Bill


Kenosis
User

Nov 23, 2013, 9:16 AM

Post #5 of 6 (1221 views)
Re: [tkrason] script open and count files [In reply to] Can't Post

It's not necessary to use the module File::Find, in this case, as directory recursion's not needed. This is why BillKSmith suggested using glob for obtaining the list of text files in the specified directory.

Given the above, consider the following:

Code
use strict; 
use warnings;

my $dir = '.';
my $count = 0;

for (<$dir/*.txt>) {
push @ARGV, $_;

while (<>) {
$count++ if /\bTEST\b/;
}
}

print $count;


The <$dir/*.txt> notation globs all text files in the directory $dir, returning a list of file text file names. The for loop implicitly assigns each one of these file names to Perl's default scalar, $_. The file name in $_ is pushed onto @ARGV, so we can let Perl take care of opening and closing the file for reading. The while (<>) { notation iterates through each file's lines (<> means <ARGV> and we just placed the file's name in @ARGV). The regex /\bTEST\b/ (implicitly matched against $_, in this case) is used to match the word on each line. Note that the search word is enclosed by \b; this means word boundary, since it seems that you don't want an in-string match. The variable $count is incremented for each successful match. Finally, the variable $count is printed.

If the above is just all too unfamiliar at this point, here's a functionally equivalent alternative:

Code
use strict; 
use warnings;

my $dir = '.';
my $count = 0;

my @textFiles = <$dir/*.txt>;

for my $textFile (@textFiles) {
open my $fh, '<', $textFile or die $!;

while ( my $line = <$fh> ) {
$count++ if $line =~ /\bTEST\b/;
}

close $fh;
}

print $count;


The close $fh; is optional, in this case, because Perl will automatically close the current file when a new file is assigned to the file handle $fh in the three-argument open statement.

Hope this helps!


(This post was edited by Kenosis on Nov 23, 2013, 9:39 AM)


Laurent_R
Veteran / Moderator

Nov 24, 2013, 4:45 AM

Post #6 of 6 (1202 views)
Re: [tkrason] script open and count files [In reply to] Can't Post

Hi,

you could also use one of the following Perl one-liners, which count the number of occurrences of "use strict" in the *.pl files of the current directory:



Code
$ perl -ne '$count++ if /use strict/; END  { print $count, "\n"}' *.pl 
71

$ perl -e 'while (<>) {$count++ if /use strict/; } print $count, "\n";' *.pl
71


These will work on Unix, Linux or Cygwin, but probably not under Windows where a couple of changes would be needed.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives