I am looking for a simple batch solution for the following:
In a number of files I want to replace a string with another one. Can do this with 开发者_开发百科Notepad++. However, each new string has to be unique, read from a list of new strings.
So, if 'abc' occurs in some files, and I have a list with new strings, replace it this way:
abc --> alex
abc --> ben abc --> chris abc --> dave etc.I can have a txt file with the new strings to read from.
Hope someone has a solution for me!
Many thanks, Lennart
Would a Perl script work?
my @words = qw(alex ben chris dave ...);
while (<>) {
s/abc/shift @words/ge;
print;
}
If you want the word list to loop:
my @words = qw(...);
my $i = 0;
while (<>) {
# I know I should have written this in a more readable way...
s{abc}{$words[$i++] // $words[$i=0]}ge;
print;
}
Or as batch script
@echo off
setlocal Disabledelayedexpansion
set "wordlist=alex ben chris dave"
for /F "tokens=* delims=" %%a in (myFile.txt) do (
set "line=%%a"
setlocal Enabledelayedexpansion
for /F "tokens=1,*" %%b in ("!wordlist!") do (
set "newline=!line:abc=%%b!"
(echo(!newline!)
if !newline! NEQ !line! (
endlocal
set "wordlist=%%c"
) ELSE (
endlocal
)
)
)
Edit Change to a "!" safe variant
Try and find a sed
executable for Windows which supports the -i
(--inline
) switch and your job will become a toddler's task!
精彩评论