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
Post a Comment