Home > Archive > PERL Beginners > April 2004 > Error in Script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
|
Hi,
I have wrote a script to search for a pattern and replace it in
all files of a directory,that i specified at commandline.
I want another one thing is to be done in the script.That's,it should
search only for the type of files I specified at commandline.
That is,it should get the extension of file name and search only those
files in the directory.
I have made a attempt in this script,but it goes on asking extension for
all the files in directory.
Plz help me in getting the things right in the following script.
Here I have use Split operator to get the extension of filename.
#! /usr/bin/perl
print "Enter a path name: ";
my $path=<STDIN>;
chomp($path);
opendir THISDIR, "$path" or die "serious dainbramage: $!";
my @allfiles = readdir THISDIR;
# get all files
foreach $file (@allfiles){
$filetoopen = $path ."/" .$file;
# filter to check the type of file
print "Enter the type of extension:";
my $ext=<STDIN>;
chomp($ext);
($str1,$str2) = split(/./, $filetoopen);
if($str2 eq $ext)
{
print $str2;
print $filetoopen;
open(IN, "<$filetoopen") || die "cannot open file\n";
open(OUT, ">$test") || die "cannot open file\n";
while (<IN> ){
if (/$com/){
s/$com/td>\n<\\script>/g;
}
if (/$img/){
s/$img/\n<script>\n<img/g;
}
if (/$pattern/){
s/$pattern/$own/g;
# print $_;
}
if (/img/){
s/$img/document.write("<img/g;
}
if (/$com/){
s/$com/td>");/g;
}
print OUT $_;
}
close (OUT);
close (IN);
rename("$test","$filetoopen");
}}
Thanks inadvance,
Prabu.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.620 / Virus Database: 399 - Release Date: 3/11/2004
| |
| Martin Lercher 2004-04-04, 8:30 am |
| That's 'cause you have put the question inside the foreach loop:
> # get all files
> foreach $file (@allfiles){
> $filetoopen = $path ."/" .$file;
>
> # filter to check the type of file
> print "Enter the type of extension:";
> my $ext=<STDIN>;
Btw, it would be easier to just use the fileglob operator from the start
(with a pattern of the form "path/*.ext"):
#! /usr/bin/perl
print "Enter a glob pattern:";
my $pattern=<STDIN>;
chomp($pattern);
my @files = glob ($pattern) ;
@files or die "serious dainbramage: $!";
foreach my $file (@files) {
print $filetoopen;
open(IN, "<$filetoopen") || die "cannot open file\n";
....
Martin
| |
| John W. Krahn 2004-04-04, 10:31 am |
| Prabu wrote:
>
> Hi,
Hello,
> I have wrote a script to search for a pattern and replace it in
> all files of a directory,that i specified at commandline.
> I want another one thing is to be done in the script.That's,it should
> search only for the type of files I specified at commandline.
> That is,it should get the extension of file name and search only those
> files in the directory.
>
> I have made a attempt in this script,but it goes on asking extension for
> all the files in directory.
That is because you are asking for the extension inside the loop. Just
ask before you enter the loop.
> Plz help me in getting the things right in the following script.
> Here I have use Split operator to get the extension of filename.
>
> #! /usr/bin/perl
use warnings;
use strict;
> print "Enter a path name: ";
> my $path=<STDIN>;
> chomp($path);
> opendir THISDIR, "$path" or die "serious dainbramage: $!";
perldoc -q vars
> my @allfiles = readdir THISDIR;
>
> # get all files
> foreach $file (@allfiles){
> $filetoopen = $path ."/" .$file;
>
> # filter to check the type of file
> print "Enter the type of extension:";
> my $ext=<STDIN>;
> chomp($ext);
Move the previous four lines outside of the foreach loop.
> ($str1,$str2) = split(/./, $filetoopen);
The . character is special in a regular expression, it matches all
characters except the newline character.
> if($str2 eq $ext)
> {
> print $str2;
> print $filetoopen;
> open(IN, "<$filetoopen") || die "cannot open file\n";
> open(OUT, ">$test") || die "cannot open file\n";
Where did $test come from? You are using $! in your opendir() error
message, you should use it here as well.
> while (<IN> ){
> if (/$com/){
> s/$com/td>\n<\\script>/g;
> }
> if (/$img/){
> s/$img/\n<script>\n<img/g;
> }
> if (/$pattern/){
> s/$pattern/$own/g;
Where did $com, $img, $pattern and $own come from? There is no need to
do a match before you do a substitution with the same regular
expression.
> # print $_;
> }
> if (/img/){
> s/$img/document.write("<img/g;
> }
> if (/$com/){
> s/$com/td>");/g;
You have already removed $img and $com above. What did you think these
substitutions would accomplish?
> }
> print OUT $_;
> }
> close (OUT);
> close (IN);
> rename("$test","$filetoopen");
perldoc -q vars
You should verify that rename() succeeded.
> }}
----------------------------------------
#!/usr/bin/perl
use warnings;
use strict;
print 'Enter a path name: ';
chomp( my $path = <STDIN> );
print 'Enter the type of extension: ';
chomp( my $ext = <STDIN> );
opendir THISDIR, $path or die "serious dainbramage: $!";
my @allfiles = map "$path/$_", grep /\Q$ext$/, readdir THISDIR;
# get all files
foreach my $filetoopen ( @allfiles ) {
print $filetoopen;
open IN, "<$filetoopen" or die "cannot open file: $!";
open OUT, ">$test" or die "cannot open file: $!";
while ( <IN> ) {
s/$com/td>\n<\\script>/g;
s/$img/\n<script>\n<img/g;
s/$pattern/$own/g;
s/$img/document.write("<img/g;
s/$com/td>");/g;
print OUT $_;
}
close OUT;
close IN;
rename $test, $filetoopen or warn "Cannot rename $test to
$filetoopen: $!";
}
__END__
John
--
use Perl;
program
fulfillment
| |
| David Kirol 2004-04-05, 6:30 am |
|
You'll need to fix this also
> ($str1,$str2) = split(/./, $filetoopen);
It should be:
($str1,$str2) = split(/\./, $filetoopen);
So that split can funcrion as you expect.
|
|
|
|
|