Let say, in my home directory I have a directory called “document”. In the directory, all the files name end with .doc (e.g: notes1.doc). The question is how can I write a script to extract the info from all the .doc file and print it in a file (let say the file called “result”). the concept i use is:-
1) open the directory "document"
2) search for all the .doc file (pattern matching)
3) create file "result"
4) extract info from all .doc file
5) print to the extracted info into result
this is the 1st to open the directory before i continuew to next step. is this the correct code?
#!/usr/bin/perl
$dirname = "/eng/png/home/awhf/document";
opendir(DIR, "<$dirname")or die "Cannot open directory";
while(<DIR>)
{
.....
.....
.....
#!/usr/bin/perl
use strict;
use warnings;
my $dirname = "/eng/png/home/awhf/document";
chdir ($dirname) or die "Can't chdir to $dirname: $!";
opendir(DIR, $dirname) or die "Cannot open directory: $!";
my @files = grep {/\.doc$/} DIR;
close DIR;
open (OUT,">>results.txt") or die "Can't open results.txt: $!";
{
local @ARGV = @files;
while (<>) {
print OUT;
}
}
close OUT;