Replacing specific text from document A to B
-
Hi there, I was wondering if anyone can help me do this:
I have two documents opened side by side (as shown below):

I am trying to get the already-translated text from document A (on the left) to the respective lines in document B. I cannot just copy-paste this, because I know there might be some lines that can have different “keys” and are on different lines.
The thing is (as you can see on the screenshot) I cannot just select lines from document A and paste them into document B, because they are on the different lines.My question is: Is there a way to replace text in the document B if it matches the “key” in the document A? Such as (as for the screenshot):
- I have a key called “searchInputHintOnTable” in the document A. If there is the same key in the document B, use the text ‘Search by alert name’ (from document A) to replace the ‘Hledat podle názvu’ (in document B).
I hope I explained it well enough, but I will be more than happy to specify this as much as possible.
Thank you.
-
You really should follow the instructions HERE for posting such a question.
-
Hello, @Václav-Trunec and All,
I’ve got a solution to your problem which is a bit long, but without any major difficulty !
It will use :
-
5regex search/replacements -
2sort operations -
The use of the
column editorto number the lines
I assume that your initial
Document Acontains only lines like :Key: 'English strings',and that your initial
Document Bcontains only lines like :Key: 'Czech strings',A last advice : Better to read carefully all this post first, before beginning any operation !!
Regarding the
5regex S/R, you’ll follow these steps :-
Open the Replace dialog (
Ctrl + H) -
Type in the SEARCH regex in the
Find what:field -
Type in the REPLACE regex in the
replace with:field -
Untick all options
-
Tick the
Wrap aroundoption -
Select the
Regular expressionsearch mode -
Click preferably on the
Replace Allbutton ( Do not use theReplacebutton in the fourth regex S/R )
Let’s go :
-
Copy/paste the
Document Acontents in a newDocument C -
Use this first regex S/R, in
Document C:-
SEARCH
^(\w+): -
REPLACE
\1 CCC:
-
=> Contents of
Document Cshould look as below :... ... searchInputHintOnTable CCC: 'Search by alert name', searchInputHint CCC: 'Find the alert belonging to a specific alert by its name.', ... ...-
Copy/paste the
Document Bcontents in a newDocument D -
Use this second regex S/R, in
Document D:-
SEARCH
^(\w+): -
REPLACE
\1 DDD:
-
=> Contents of
Document Dshould look as below :... ... searchInputHint DDD: 'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu', searchInputHintOnTable DDD: 'Hledat podle nazvu alertu', ... ...-
Now, try to find out the longest line of
Document D. Its length will be set to theLmvariable -
Move to the first line of
Document D. Its length will be set to theL1variable -
Determine the value
X=Lm - L1 + 5 -
Move at the end of the first line of
Document D -
Add
X spacechars -
Open the Column Editor (
Alt + C)-
Choose the
Number to Insertoption -
Type in the value
1in theInitial number :field -
Type in the value
1in theIncrease by :field -
Type in the value
1in theRepeat :field -
Tick the
Leading zerosoption ( IMPORTANT ) -
Verify that the
Decoption is selected -
Click on the
OKbutton
-
=> A numbering should appear at the end of each line of
Document D... ... searchInputHint DDD: 'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu', xxxx searchInputHintOnTable DDD: 'Hledat podle nazvu alertu', yyyy ... ...- Merge
document CandDocument Din a newDocument E
=> Contents of
Document Eshould look as below :... ... searchInputHintOnTable CCC: 'Search by alert name', searchInputHint CCC: 'Find the alert belonging to a specific alert by its name.', ... ... searchInputHint DDD: 'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu', xxxx searchInputHintOnTable DDD: 'Hledat podle nazvu alertu', yyyy ... ...-
Now, in
Document E:-
Suppress any selection
-
Use the
Edit > Line Operations > Sort Lines Lexicographically Ascendingoption
-
=> The contents of
Document Ebecome :... ... searchInputHint CCC: 'Find the alert belonging to a specific alert by its name.', searchInputHint DDD: 'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu', xxxx .... .... searchInputHintOnTable CCC: 'Search by alert name', searchInputHintOnTable DDD: 'Hledat podle nazvu alertu', yyyy .... ....-
Use this third regex S/R, in
Document E:-
SEARCH
(?-is)^(.+) CCC: (.+)\R\1 DDD: \K.+, -
REPLACE
\2
-
=> The contents of
Document Eare now changed into :... ... searchInputHint CCC: 'Find the alert belonging to a specific alert by its name.', searchInputHint DDD: 'Find the alert belonging to a specific alert by its name.', xxxx ... ... searchInputHintOnTable CCC: 'Search by alert name', searchInputHintOnTable DDD: 'Search by alert name', yyyy ... ...-
Use this fourth regex S/R, in
Document E:-
SEARCH
(?-is)^.+ CCC: .+\R|^(.+ DDD: .+?)\x20+(\d+)$ -
REPLACE
(?1\2 \1:)
-
=>
-
All the lines containing the
CCC:string, surrounded byspacechars, should be deleted : -
Numbering, in lines containing the
DDD:string, surrounded byspacechars, should have moved from the end to the beginning of each line -
Remember that
xxxxandyyyyare numbers !
... ... xxxx searchInputHint DDD: 'Find the alert belonging to a specific alert by its name.', ... ... yyyy searchInputHintOnTable DDD: 'Search by alert name', ... ...-
Again, in
Document E:-
Suppress any selection
-
Use the
Edit > Line Operations > Sort Lines Lexicographically Ascendingoption
-
=> This time, all the lines should be sorted, according to their initial location in
Document BandDocument D... ... xxxx searchInputHint DDD: 'Find the alert belonging to a specific alert by its name.', yyyy searchInputHintOnTable DDD: 'Search by alert name', ... ...-
Use this fifth and final regex S/R, in
Document E:-
SEARCH
(?-i)^\d+\x20{4}|\x20DDD(?=:\x20) -
REPLACE
Leave EMPTY
-
Your final
Document Eshould look like below :... ... searchInputHint: 'Find the alert belonging to a specific alert by its name.', searchInputHintOnTable: 'Search by alert name', ... ...-
The order of lines, in
Document E, should be identical to theDocument Border -
The
Czechversion of all lines, inDocument BandDocument E, should have been replaced with theirEnglishequivalents !
Best Regards
guy038
-
-
First of all, thank you very much for the thorough explanation, it is absolutely wonderful.
However, I am stuck at this thing in Document E:
- Use the Edit > Line Operations > Sort Lines Lexicographically Ascending option
If I do this, the document will not be usable for our developers, since it also moves the { and } characters, determining the end of a translation key. Is there a way to skip this?
Thank you again.
-
@václav-trunec said in Replacing specific text from document A to B:
If I do this, the document will not be usable for our developers, since it also moves the { and } characters, determining the end of a translation key. Is there a way to skip this?
If you follow Guy’s full instructions, you will see that he added a “key” (the extra text) which will allow his two different sorts to undo each other: he sorts on that “key”, and then in the end, it re-sorts back to the original order. As Guy said, “The order of lines, in
Document E, should be identical to theDocument Border” after his last step.-----
Alternatively, I recently published a script in a similar discussion, which will allow you set up pairs of English/OtherLanguage translation strings (or, in your case, OtherLanguage-to-English). If you follow the instructions in that post, but use the original/Czech as the left side of the translation dictionary and the translated/English as the right side, it should work.
So, instead of using the example from that discussion:
translation = { r'"JJIS number:"': r'"د JJIS شمیره:"', r'"Choose one"': r'"یو يې غوره کړئ"', r'"Social Security number:"': r'"د ټولنیز مصؤنیت لمبر:"', r'"State ID \(SID\):"': r'"د ایالت ID \(SID\):"' }… you would use something like …
translation = { r'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu': r'Find the alert belonging to a specific alert by its name.', r'Hledat podle nazvu alertu': r'Search by alert name' }(remembering to end every pair except the last one with a comma