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

后端 未结 1 1426
死守一世寂寞
死守一世寂寞 2020-12-20 06:18

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 lik

相关标签:
1条回答
  • 2020-12-20 06:33

    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.

    0 讨论(0)
提交回复
热议问题