Reputation: 71
How do I run a Perl script on multiple input files with the same extension?
perl scriptname.pl file.aspx
I'm looking to have it run for all aspx files in the current directory.
Upvotes: 7
Views: 18671
Reputation: 189377
For a simple one-liner with -n
or -p
, you want
perl -i~ -pe 's/foo/bar/' *.aspx
The -i~
says to modify each target file in place, and leave the original as a backup with a ~
suffix added to the file to name. (Omit the suffix to not leave a backup. But if you are still learning or experimenting, that's a bad idea; removing the backups when you're done is a much smaller hassle than restoring the originals from your regular backups if you mess something up.)
Of course, if you want the Perl script to write output to standard output, and not modify the files, don't put the -i
option at all.
If your Perl code is too complex for a one-liner (or just useful enough to be reusable) obviously replace -e '# your code here'
with scriptname.pl
... though then maybe refactor scriptname.pl
so that it accepts a list of file name arguments, and simply use scriptname.pl *.aspx
to run it on all *.aspx
files in the current directory.
If you need to recurse a directory structure and find all files with a particular naming pattern, the find
utility is useful.
find . -name '*.aspx' -exec perl -pi~ -e 's/foo/bar/' {} +
If your find
does not support -exec ... +
try with -exec ... \;
though it will be slower and launch more processes (one per file you find instead of as few as possible to process all the files).
To only scan some directories, replace .
(which names the current directory) with a space-separated list of the directories to examine, or even use find
to find the directories themselves (and then perhaps explore -execdir
for doing something in each directory that find
selects with your complex, intricate, business-critical, maybe secret list of find
option predicates).
Maybe also explore find2perl
to do this directory recursion natively in Perl.
In case it's not obvious, -pi~
is a condensed way to write -p -i~
; this is called "option clustering".
Upvotes: 4
Reputation: 1
For example to handle perl scriptname.pl *.aspx *.asp
In linux: The shell expands wildcards, so the perl can simply be
for (@ARGV) {
operation($_); # do something with each file
}
Windows doesn't expand wildcards so expand the wildcards in each argument in perl as follows. The for loop then processes each file in the same way as above
for (map {glob} @ARGV) {
operation($_); # do something with each file
}
For example, this will print the expanded list under Windows
print "$_\n" for(map {glob} @ARGV);
Upvotes: 0
Reputation: 409
You can use glob
explicitly, to use shell parameters without depending to much on the shell behaviour.
for my $file ( map {glob($_)} @ARGV ) {
print $file, "\n";
};
You may need to control the possibility of a filename duplicate with more than one parameter expanded.
Upvotes: 2
Reputation: 7259
You can also pass the path where you have your aspx files and read them one by one.
#!/usr/bin/perl -w
use strict;
my $path = shift;
my @files = split/\n/, `ls *.aspx`;
foreach my $file (@files) {
do something...
}
Upvotes: -2
Reputation: 342353
you can pass those files to perl with wildcard
in your script
foreach (@ARGV){
print "file: $_\n";
# open your file here...
#..do something
# close your file
}
on command line
$ perl myscript.pl *.aspx
Upvotes: 2
Reputation: 2197
If you are on Linux machine, you could try something like this.
for i in `ls /tmp/*.aspx`; do perl scriptname.pl $i; done
Upvotes: 2
Reputation:
In your Perl file,
my @files = <*.aspx>;
for $file (@files) {
# do something.
}
The <*.aspx>
is called a glob.
Upvotes: 8