You can easily generate a sed
script from your input file ... using sed
.
sed 's/^/s%/;s/, */%/;s/$/%g/' Replace.txt | sed -f - -i /directory/*
The first command in the pipeline generates a sed
script from your replacement patterns; it looks like
s%Old%New%g
s%Apples%Bananas%g
and we then feed this to another sed
instance, specifying standard input (-
) as the file to read the script from (-f
) and the files you want to perform these replacements in as the file name arguments.
s/^/s%/
inserts s%
at beginning of line. s/, */%/
replaces the first comma (with optional trailing whitespace) with just %
. s/$/%g/
adds %g
at the end of each line. (In regex, ^
matches the beginning of line, and $
matches the end of line.)
Linux sed
happily reads a script from standard input; some other platforms may be encumbered, or require a workaround like /dev/stdin
instead of -
as the file name argument. In the worst case, save the generated script in a temporary file, then remove it when you're done.
Perhaps notice that this will happily replace PineApplesauce
with PineBananasauce
and BOldface
with BNewface
. You can perhaps devise a more constrained regular expression around the Old
text. Notice also that if Old
contains regular expression metacharacters, those should be escaped if you want sed
to replace them literally; so
s%your *new* friend%your *old* buddy%
is wrong, and should instead be something like
s%your [*]new[*] friend%your *old* buddy%
if the intent is to interpret the "old" phrase as literal text.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…