Hi Prabu, > 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. > Tell the changes to be made in this. > > #! /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; > The following lines are the problem: > # filter to check the type of file > print "Enter the type of extension:"; > my $ext=<STDIN>; > chomp($ext); You're asking for the extension inside the foreach-loop, therefore your application asks you for the extension everytime it processes a file. Simply move those lines up (in front of the "get all files" comment), and everything should work. :-) > ($str1,$str2) = split(/./, $filetoopen); > if($str2 eq $ext) > { > print $str2; > $filetoopen1 = join(".",$str1,$str2); > print $filetoopen1; > open(IN, "<$filetoopen1") || die "cannot open file\n"; > open(OUT, ">$test") || die "cannot open file\n"; > while (<IN>){ > if (/$com/){ > s/$com/td>\n</g">\\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","$filetoopen1"); > } > } > __END__ > > The problem you're trying to solve - replacing one pattern with another in multiple files - is actually a very common one for perl scripts. Thus there´s some built-in perl magic for handling scenarios like this: A) working on multiple files line per line Take the following code and save it in a perl script called multiplefiles.pl: ##### START multiplefiles.pl ##### #! /usr/bin/perl -w use strict; while (<>) { print "processing line ($_) in file $ARGV\n"; } ##### END multiplefiles.pl ##### Now call it on command line like this: multiplefiles.pl myfile1.txt myfile2.txt As you hopefully see, this code opens every file and processes it line by line... B) Globbing Adding one line, perl lets you specify wildcards on the command line: ##### STARTUP multiplefiles2.pl ##### #! /usr/bin/perl -w use strict; @ARGV = glob(join(' ', @ARGV)); while (<>) { print "processing line ($_) in file $ARGV\n"; } ##### END multiplefiles2.pl ##### Now you can call this script like this: multiplefiles2.pl myfile*.pl some*stuff.txt file2 ...and Perl will do the rest for you. By the way: I'd recommend to change the first lines of your script to: #! /usr/bin/perl -w use strict; This enables some more warnings and checks - very helpful to avoid mistakes... Also, I'd indent blocks for better readability - something like: if ($str2 eq $str1) { foreach (@values) { print "doing something.\n"; } } HTH, Philipp -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>