Editing files in place with perl from powershell -


i have list of files files.txt in directory. want run text substitution through each file.

$f = (get-content files.txt) foreach ($i in $f) { perl -pi -we "s/(\d{0,4})- /$1 - /g" $i } 

but perl doesn't without making backup.

can't inplace edit without backup.

so added .bak -i:

$f = (get-content files.txt) foreach ($i in $f) { perl -pi.bak -we "s/(\d{0,4})- /$1 - /g" $i } 

and complains:

can't open perl script ".bak": no such file or directory

what missing?

update:

what prefer way without shell entirely. can loop in perl $^i somehow?

my $qfn = 'files.txt'; open(my $fh, '<', $qfn)    or die("can't open file list \"$qfn\": $!\n");  local @argv = <$fh>; local $^i = '.bak'; local $_; while (<>) {    s/(\d{0,4})- /$1 - /g;    print; } 

\d matches more 0-9 when /a isn't used, want

 s/(\d{0,4})- /$1 - /ag;    -or-  s/([0-9]{0,4})- /$1 - /g; 

note above doesn't prevent 5 digits being matched appear want. that, use:

 s/(?<![0-9])([0-9]{0,4})- /$1 - /g; 

now, there's no reason replace number when know \k.

 s/(?<![0-9])[0-9]{0,4}\k- / - /g; 

and finally, can rid of duplicate - too.

 s/(?<![0-9])[0-9]{0,4}\k(?=- )/ /g; 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -