Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding and replacing many words
    primarykey
    data
    text
    <p>I frequently need to make many replacements within files. To solve this problem, I have created two files <code>old.text</code> and <code>new.text</code>. The first contains a list of words which must be found. The second contains the list of words which should replace those.</p> <ul> <li>All of my files use UTF-8 and make use of various languages.</li> </ul> <p>I have built this script, which I hoped could do the replacement. First, it reads <code>old.text</code> one line at a time, then replaces the words at that line in input.txt with the corresponding words from the <code>new.text</code> file.</p> <pre><code>#!/bin/sh number=1 while read linefromoldwords do echo $linefromoldwords linefromnewwords=$(sed -n '$numberp' new.text) awk '{gsub(/$linefromoldwords/,$linefromnewwords);print}' input.txt &gt;&gt; output.txt number=$number+1 echo $number done &lt; old.text </code></pre> <p>However, my solution does not work well. When I run the script:</p> <ul> <li>On line 6, the <code>sed</code> command does not know where the <code>$number</code> ends.</li> <li>The <code>$number</code> variable is changing to "0+1", then "0+1+1", when it should change to "1", then "2".</li> <li>The line with <code>awk</code> does not appear to be doing anything more than copying the input.txt exactly as is to output.txt.</li> </ul> <p>Do you have any suggestions?</p> <p>Update:</p> <p>The marked answer works well, however, I use this script a lot and it takes many hours to finish. So I offer a bounty for a solution which can complete these replacements much quicker. A solution in BASH, Perl, or Python 2 will be okay, provided it is still UTF-8 compatible. If you think some other solution using other software commonly available on Linux systems would be faster, then that might be fine too, so long as huge dependencies are not required.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload