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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>