Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform text substitution within all files in a folder?

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.

like image 785
Marcus Shepheard Avatar asked Dec 21 '22 16:12

Marcus Shepheard


1 Answers

Try this:

for /r %i in (*.xml) do perl -pi.bak -e "s/(\d+.\d+E-\d+)/2.2E-6/g;" %i

like image 149
dsolimano Avatar answered Mar 27 '23 02:03

dsolimano