44 Votes

How to Replace multiple Texts at the same Time

Tutorial by Stefan Trost | 2023-12-23 at 21:19

Sometimes a single replacement is not enough and we are faced with the task of having to replace several texts in text files at the same time. Especially when we need to perform these replacements on a large number of files, it can become very labor intensive to open each file individually in order to perform the many substitutions one after the other again and again.

It's much easier with using the TextConverter, which we would like to introduce in this tutorial. With this program it is possible to perform any number of replacements on any number of text files at once. In this tutorial we would like to look at the following cases:

In any of these cases, we can simply drag all the files in which we want to make the replacements from any folder to the TextConverter program window for editing. We can save the changes using the "Convert and Save" button at the bottom right of the main window. Apart from that, if we want to edit unsaved text, we can simply copy it into the field "Text" in order to get the result immediately in the field "Preview".

Replace multiple Texts with several different Texts

We can find the search and replace function in the TextConverter on the right side of the main window under "Actions > Text > Replace". In the options of this action there are two fields, one field for the search text and another field for the replacement text.

In addition to these fields, we are interested in the two options "Each line is a separate Search Text" and "Each line is a separate Replacement" under the two fields. If we activate both of these options, we can easily define several replacements at the same time.

For example, we could write the following text into the field "Search for":


And this text into the field "Replace with":


If both of the above options are activated, this configuration results in every "a" being replaced by an "x", every "b" by a "y" and every "c" by a "z".

The text in the first line of the search field is replaced with the text from the first line of the replace field, the text from the second line of the search field is replaced with the text from the second line of the replace field, and so on. We can define as many lines as we want, but we should make sure that both fields have the same number of lines. In addition, the individual characters "a", "b" and "c" from the demonstrations are of course only examples. We can also use longer strings including spaces on each line.

If we had not activated either of the two options mentioned, the entire multi-line search text would be replaced by the entire multi-line replacement text. A replacement would therefore only be carried out if one line ends with an "a", the next line only has a "b" and the following line begins with a "c".

The option "Interpret as Regular Expression" also allows us to apply a series of regular expressions for our replacements at the same time.

Of course, this type of configuration only works for single-line search terms and single-line replacements. You can find out how you can replace multiple multi-line texts in this section of this tutorial.

Replace multiple Texts with the same Text

To replace multiple texts with multiple different texts, we have activated both options "Each line is a separate Search Text" and "Each line is a separate Replacement". But what happens if we only activate one of these options and the other option is not active?

If we only activate the option "Each line is a separate Replacement", we can define multiple texts in form of lines in the search field, which will then all be replaced with the same text from the replace field.

If we, for example, enter the following text into the search field...


...and we write this text into the replace field...


...all occurrences of "a", "b" or "c" in our original text are replaced by an "x".

Again, also in this application case, we could work with regular expressions, enter any number of lines in the search field or use any other longer text in the respective lines.

Replace one Text with multiple Texts

Of course it also works the other way around. If we only activate the option "Each Line is a separate Replacement" in the text replace action while leaving the option "Each Line is is a separate Search Text" option disabled, we can replace one text with several texts.

Let's say we enter the following text into the search field:


And as replacement, the following 3 lines in the replace field:


In this case, the first occurrence of "a" in our original text is replaced by the first line, so by "x". The second occurrence of "a" is replaced by the next line, which is "y" here. And if there is another and therefore third "a" appearing in the original text, it will be replaced by the "z" written in the third line.

If a fourth "a" should appear in our original text, it will remain as such because we have not defined a fourth line for further replacements. This also applies to every additional "a" whose "number" exceeds the number of lines.

Replace multiple multi-line Texts

All previous types of replacements introduced in this tutorial could be easily solved using the action "Text > Replace" and activating the options “Each Line is a separate Replacement” respectively “Each Line is a separate Search Text”.

However, once we have activated this option for a text field, we can no longer use the text field in question to replace multi-line text. If only one of the two options is activated, we can still work with line breaks in the other text field, but as soon as both options are activated or we need line breaks in both text fields, we need a different solution if we want to replace several multi-line texts at the same time.

The solution is called: Action Lists. With action lists we can group multiple actions and execute them together at the same time. We can configure an action list in the TextConverter under "Actions > Text > Action Lists".

The action for replacing text is called "REPLACE" and can be created either via the graphical user interface using the action list editor or in the form of a text. As text, the "REPLACE" action has the following syntax:

REPLACE Search Replacement

An action is always defined in one single line and is introduced by the word of the action, here "REPLACE". This is followed by the parameters, which are separated from each other with a space. The first parameter is the search word, the second parameter is the replacement.

If the search word and/or the replacement contains spaces, we can use quotation marks:

REPLACE "Old Word" "New Word"

If we want to perform multiple replacements simultaneously, we can simply create an action list with one replacement per line. So for example:


This action list has the same effect as the example from the first section of this tutorial, which we implemented using the function "Text > Replace": every occurrence of the letter "a" is replaced by "x", "b" by "y" and "c" by "z".

With this, we have now almost everything together we need to define multiple replacements using action lists. However, the only thing still missing is how we can implement replacements containing line breaks within the action lists.

Since we only have a single line available to define an action within an action list, we cannot simply enter a line break using the Enter key. Rather, for the Windows line break, we need to use the character string "\r\n" instead of a "real" line break.

An example for this is the following action list with three replacements:

REPLACE abc\r\ndef uvw\r\nxyz
REPLACE ghi\r\njkl "ghi jkl"
REPLACE 123 1\r\n2\r\n3

With the first line of our action list, we replace the string "abc" followed by a newline and the string "jkl" with the string "uvw" + newline + "xyz". With the second line, we replace every occurrence of a line break between the strings "ghi" and "jkl" with a space. And with the third and last line of our action list, we insert a line break between each of the numbers in the string "123".

If you don't want to enter line breaks in this way, you can of course also use the editor to create your action lists. If you create an action within the editor or with using the button "New Action", you can enter line breaks in the usual way using the Enter key and the TextConverter will take care of the rest.

Finally, for the sake of completeness, we want to have a look at how you can define replacements in your action lists using regular expressions or considering the uppercase and lowercase writing. For this we use the third and optional parameter of the "REPLACE" function:

REPLACE [0-9] X -r
REPLACE [a-f] X -cr

If we use "-r" as the third parameter, the replacement will be interpreted as a regular expression. We see an example of this in the first line of the action list above. With this line we replace all digits from 0 to 9 with an "X".

If we use the "-c" parameter, the replacement will be carried out case-sensitive. In the second and third line of this action list, we replace "a" with "X". Once with the "-c" parameter and once without. The second line only replaces the letter "a" with the "X" because we have only defined the small "a" and use "-c" to configure that the writing should be respected. In the third line we omit the "-c", resulting in replacing both "a" and "A" with "X".

Of course, a combination of these two parameters is also possible. The fourth line demonstrates how this works. Here we use the regular expression [a-f] to define that the letters a to f should be replaced by X. Only these lowercase letters should be replaced and not the letters A to F. If we had omit the "c", the letters A to F would also be replaced by "X".

Limit Replacements to specific Areas of Text

All the replacements we have looked at in the examples and sections of this tutorial so far have always been applied to the entire text we are editing. However, with the TextConverter, it is also possible to limit the scope of the replacements to certain areas of a text or a text file.

Under "Actions > Lines > Replace in Lines" you can for example find the option to carry out replacements only in freely definable lines of the text. You can find similar actions under "Actions > CSV > Replace in Fields" or under "Actions > XML". These features allow you to limit your replacements to specific columns or fields of CSV data, or apply them only to specific inner texts, tag names, attribute names, or attribute values within an XML structure.

Both the line actions as well as the CSV actions and the XML actions include the ability to define action lists in the same way as we saw in the previous section. This means that multiple replacements can be defined and applied simultaneously for each of these areas.

If you need an even more precise selection of your replacement area, you can also work with the action list selectors (the respective SELECT or DESELECT functions of each action type), which allow almost any type of selection of rows, fields, elements, or other text sectors. Just try out the functions and possibilities of the TextConverter and ask us if you should encounter any question.


About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile


Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.