I need to update a single parameter within a large set of .xml files. I read around and using a single line of perl at the windows command line I can get it to do substitutions a file at a time, but I am struggling to get perl to do all the files at once.
perl -pi.bak -e "s/(\d+\.\d+E-\d+)/2.2E-6/g;" test.xml
This works, however when I attempt to change it to something like
perl -pi.bak -e "s/(\d+\.\d+E-\d+)/2.2E-6/g;" *.xml
开发者_如何学编程
I get "Can't open *.xml: Invalid argument"
perl -pi.bak -e "s/(\d+\.\d+E-\d+)/2.2E-6/g;" .xml
Gives me "Can't open .xml: No such file or directory"
I tried to see if I could call it from within another perl script using system(), however that seems to take issue with the use of quotation marks, and may not the best way to do this.
Summary:
Problem - I have a large number of .xml files within which I want to change a single parameter. I have to be able to do this on a windows machine and ideally I would like to work towards a solution that would enable me to automate this in a script so I can loop through numerous parameter value substitutions (with a call to a separate program that takes the .xml files as an input in between substitutions).
Thanks for any help you can offer.
Try this:
for /r %i in (*.xml) do perl -pi.bak -e "s/(\d+.\d+E-\d+)/2.2E-6/g;" %i
You can use a for
loop in a .cmd
script. Something like:
for %%a in (*.xml) do (
perl -i.bak ... %%a
)
Here's a handy cmd.exe
reference.
When I do some like this, I typically use the following pattern:
ls *.xml | xargs perl -i.bak -p -e "s/<search>/<replace>/g"
On Windows, you should be able to use the following pattern:
for %f in (*.xml) do perl -i.bak -p -e "s/<search>/<replace>/g" "%f"
See Perl 5 by Example: Using the -n and -p Options for further details.
use File::Find;
UPDATE: you can use this one-liner:
perl -we 'use File::Find; my $dir = shift; find (sub { $_=~ /\.xml$/i and system @ARGV, $_ }, $dir)' <dir> <other-one-liner>
The problem is that the Windows excuse for a shell does not do filename globbing (expanding *.xml into all files with a .xml extension).
Since the shell won't do it for you, you have to do it yourself.
This is easy to do in Perl, but it needs to be done before the "inner" code starts, so put it into a BEGIN block:
perl -pi.bak -e "BEGIN{@ARGV=glob shift} s/(\d+\.\d+E-\d+)/2.2E-6/g;" *.xml
find . -type f -name *.xml | xargs perl -pi.bak -e "s/(\d+\.\d+E-\d+)/2.2E-6/g;
"
this will pass all files under . (recursively) matching .xml to the perl command, one at a time.
精彩评论