How to run a Perl script on multiple input files with the same extension?

How to run a Perl script on multiple input files with the same extension?

perl scriptname.pl file.aspx 

I am looking for it to run for all aspx files in the current directory

Thank!

+6
command-line perl
Nov 11 '09 at 0:18
source share
7 answers

In your Perl file

  my @files = <*.aspx>; for $file (@files) { # do something. } 

<*.aspx> is called glob .

+7
Nov 11 '09 at 0:21
source share

you can pass these files to perl using a wildcard

in script

 foreach (@ARGV){ print "file: $_\n"; # open your file here... #..do something # close your file } 

on the command line

 $ perl myscript.pl *.aspx 
+3
Nov 11 '09 at 1:43
source share

You can explicitly use glob to use shell options, regardless of shell behavior.

 for my $file ( map {glob($_)} @ARGV ) { print $file, "\n"; }; 

You may need to control the possibility of duplicating a file name with more than one parameter.

+2
Nov 11 '09 at 14:41
source share

If you are on a Linux machine, you can try something like this.

 for i in `ls /tmp/*.aspx`; do perl scriptname.pl $i; done 
+1
Nov 11 '09 at 1:29
source share

For a simple single line with -n or -p you want

 perl -i~ -pe 's/foo/bar/' *.aspx 

-i~ says to change each target file in place and leave the original as a backup with the suffix ~ added to the file name. (Omit the suffix so that it doesn’t leave a backup. But if you are still learning or experimenting, this is a bad idea: deleting backups when you are done is a much smaller problem than restoring originals from a backup if you have something messed up.)

If your Perl code is too complicated for a single line (or useful enough for reuse), replace -e '# your code here' with scriptname.pl ... although it may be refactoring scriptname.pl to accept a list of file name arguments and just use scriptname.pl *.aspx to run it in all *.aspx files in the current directory.

If you need to overwrite the directory structure and find all files with a specific name pattern, it is useful to use the find utility.

 find . -name '*.aspx' -exec perl -pi~ -e 's/foo/bar/' {} + 

If your find does not support -exec ... + , try with -exec ... \; although it will be slower and start more processes (one for each file that you find, and not as small as possible to process all the files).

To only scan some directories, replace . (which calls the current directory) a space-separated list of directories to examine or even use find to find the directories themselves (and then perhaps examine -execdir to do something in every directory that find selects using complex, complex, critical important to the business, possibly a secret list of find option predicates).

It is also possible to examine find2perl to do this directory recursion natively in Perl.

0
Jun 25 '17 at 10:15
source share

For example, to process perl scriptname.pl *.aspx *.asp

On linux: the shell extends wildcards, so perl might just be

 for (@ARGV) { operation($_); # do something with each file } 

Windows does not extend wildcards, so expand the wildcards in each argument in perl as follows. Then the for loop processes each file in the same way as above.

 for (map {glob} @ARGV) { operation($_); # do something with each file } 

For example, this will print an extended list under Windows

 print "$_\n" for(map {glob} @ARGV); 
0
Jul 18 '17 at 3:24
source share

You can also pass the path where you have the 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... } 
-2
Nov 11 '09 at 5:40
source share



All Articles