Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

sed - Linux, find replace on a folder of files using a list of items for replacement?

I'm looking for a way to use an external list (Replace.txt) to replace text on a folder of 50 files in /temp.

My replacement file Replace.txt will be something like the following but contain 400+ items.

Old,New
Apples, Oranges
Mellon, Bananas
Car, Train

e.g.

I could use sed but adding my list manually would not be efficient. I don't know if you can use an external file list with similar code for sed.

Code:

sed -i 's/item1/itemb/g:s/itemc/itemd/g:s/iteme/itemf/g' *

Anyone have anything useful?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...