Replace bookmarked lines from one file to bookmarked lines in another file.
-
I have 2 files.
The first file contains lines like so<name>NAME-1</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>NAME-2</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>NAME-3</name>
ETC…
The second file contains<name>NAME 1</name> <name>NAME 2</name> <name>NAME 3</name> <name>NAME 4</name> <name>NAME 5</name>
I have each line I would like bookmarked in each file. So for file one every line containing <name>NAME-#</name> is bookmarked, and in the second file every line is bookmarked.
So what I am trying to do is get each bookmarked line in the second file to replace each bookmarked line in the first file.
Everything I have tried has failed to work. So im wondering if anyone here can help me out.My Debug Info:
Build time : Nov 13 2021 - 15:20:19 Path : C:\Program Files (x86)\Notepad++\notepad++.exe Command Line : Admin mode : OFF Local Conf mode : OFF Cloud Config : OFF OS Name : Windows 10 Enterprise (64-bit) OS Version : 2009 OS Build : 19042.1348 Current ANSI codepage : 1252 Plugins : mimeTools.dll NppConverter.dll NppExport.dll ``` Thank You!
-
@n0stal6ic said in Replace bookmarked lines from one file to bookmarked lines in another file.:
So what I am trying to do is get each bookmarked line in the second file to replace each bookmarked line in the first file.
Everything I have tried has failed to work. So im wondering if anyone here can help me out.I do have an idea that I think would work.
Steps to take would be:
- Number ALL the lines in the first file, leading zeroes ticked. This should leave the bookmarks intact.
- Copy all the bookmarked lines in the first file to another tab.
- Apply a new line number to these lines starting with 1, incrementing by 2, leading zeroes ticked, so all odd numbers.
- Number each of the lines in the second file, but starting with 2, incrementing by 2, leading zeroes ticked, so all even numbers.
- Add the second file to the new tab (bookmarked lines that were copied) and sort by line numbers. Now the lines will be a 1 to 1 relationship.
- Use regular expression to combine the line pairs and remove the line numbering, leaving the original line number in step #1.
- Copy the new lines back into the first file and re-sort.
- Use regular expression to overwrite the old line with the new line. Also remove all the line numbering added for these steps.
It might seem like a lot of steps but each is a fairly simple operation. One possible issue will be confirming that step #7 places ALL new lines after (or before) their original line. A method to force that might be to add “a” to to all line numbering in step #1 and to change the “a” to a “b” when processing the new tab in step #5/6. This would also make it easier to find the line pairs and do the replacement in step #8.
Terry
-
@terry-r said in Replace bookmarked lines from one file to bookmarked lines in another file.:
Copy all the bookmarked lines in the first file to another tab.
Actually there is a slightly better option. That is to cut the bookmarked lines from first file into a new tab. After all the subsequent steps it can be inserted back into the original file and re-sorted. So step #8 is all but eliminated.
Terry
-
Hey, thanks for the response. although, I dont think I fully understand what you mean by most of these sollutions. Im not good with n++ and Im not so sure what I should try. Ive tried to follow your setps but im pretty sure I get messed up somewhere in them. If you could maybe refraze what you are saying or maybe send a video or something that would be great! Thank you for the help and responding, just a little confuzed :D!
-
@n0stal6ic said in Replace bookmarked lines from one file to bookmarked lines in another file.:
Ive tried to follow your setps but im pretty sure I get messed up somewhere in them. If you could maybe refraze what you are saying or
You didn’t say what steps are causing you problems. So I’ll expand on all the steps, hopefully you can follow them:
First tab has the original file (#1), second tab has the replacement strings, 1 per line (#2).
For each of the tabs (#1 & #2) select the left most position in line 1. Select Edit, Column editor. Select text to add and type in a space, then again select column editor and for the tab (#1) select number to insert using 1 as the initial number, increase by 2 and leading zeros ticked. For the tab (#2) select 2 as the initial number, increase by 2 and leading zeros ticked. Both tabs have increasing numbers separated by a space from the original text on each line.
For tab (#1), cut the (previously) bookmarked lines and paste them into a new tab (#3).
On tab (#3) we need to again add a line number using Column Editor (cursor in 1st position), adding first a space, then a number, again start at 1, increase by 2 and leading zeros ticked.
Copy the contents of tab #2 to the bottom of tab #3.
Edit, Line Operations, Sort the lines as integer ascending. Then add a blank line to the bottom of tab (#3), a new last line. See the image on how it should look:
It is a simple matter to merge the 2nd line of each pair into position in the first line, and removing the first number on the first line of the pair. Using the Replace function we have
Find What:(?-s)^\d+\h(\d+\h)([^<]+).+\R[^<]+(.+\R)
Replace With:\1\2\3
click on “Replace All” button. Remove last empty line.
Copy the contents of tab #3 back to tab #1.
Edit, Line Operations, Sort the lines as integer ascending. See image on how this will look:
Remove the numbers and space at the start of each line. Using the Replace function we have
Find What:(?-s)^\d+\h
Replace With: empty field here, nothing, click on “Replace All”Tab (#1) should now contain the updated information.
Hopefully this should be easier to understand. You should perform these steps on copies of your files just in case you go astray at some point. I’m using regular expressions so your search mode must be “regular expression”.
Terry
-
@terry-r
This worked! Thank you so much for your help, I really appreciate it. -
Hi, @n0stal6ic, @terry-R and All,
@n0stal6ic, I’m wondering ? Do you really have syntax as
Name 1
in file2
and the syntax asName-1
in file1
and so on … or is it part of a more complex stuff ?And also, are all values, in file
2
, consecutive, beginning at1
? Or some values may be absent from this file2
?Best Regards,
guy038
-
@guy038 I changed the name on here when publishing, so it is sort of more complex names in the first file, (note each name was formatted as 321-NAME-123 so it was easy to edit out what i wanted to change because they were formated as such for all of them) what you do in notepad++ would have no effect on the names so it shoud not matter what is in there. The second file is consecutive yes, as I just bookmarked all rows in the first file containing “<name>” and copied them over to the second file, where I then did my edits in bulk to the names (because they were all the same. What I did is find each value with “NAME-” and replaced it with " 123" to remove the information which did it in bulk.
-
Hello, @n0stal6ic, @terry-r and All,
I wanted to point out that, if the numbering is identical between the two files, a simple regex S/R, on
file 1
, should be enough.So, no need about a
file 2
, containing the new values, no need of a temporaryfile 3
and so on !
For instance, given the
file 1
INPUT text, below :<Placemark> <name>NAME-1</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>NAME-2</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>NAME-3</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>NAME-4</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark>
Then the regex S/R, below, applied to
file 1
:SEARCH
<name>\KNAME-(\d+)(?=</name>)
REPLACE
NAME \1
would produce this text :
<Placemark> <name>NAME 1</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>NAME 2</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>NAME 3</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>NAME 4</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark>
A second example, with an other regex S/R :
SEARCH
<name>\KNAME-(\d+)(?=</name>)
REPLACE
ABC-\1-XYZ
which would give this text :
<Placemark> <name>ABC-1-XYZ</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>ABC-2-XYZ</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>ABC-3-XYZ</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>ABC-4-XYZ</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark>
Now, if I assume that your
file 1
contains only four values (1
,12
,123
and1234
), here is a last example with a third regex S/R :SEARCH
<name>\KNAME-(\d+)(?=</name>)
REPLACE
{_000\1_}
You’re left with that text :
<Placemark> <name>{_0001_}</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>{_00012_}</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>{_000123_}</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark> <Placemark> <name>{_0001234_}</name> <styleUrl>Stuff</styleUrl> <Point> <coordinates> Stuff </coordinates> </Point> </Placemark>
Note that only the
Replace
regex is different, in each example !Of course, if mathematical operations must occur on values, in
file 1
, in order to get new values, the regex approach is useless and needs scripting !Best Regards,
guy038
-
Hi,
I’m trying to adapt this to my own use case. However, I ran into the issue with:
@Terry-R said in Replace bookmarked lines from one file to bookmarked lines in another file.:
Find What:(?-s)^\d+\h(\d+\h)([^<]+).+\R[^<]+(.+\R)
I tried Replace All as suggested, but it didn’t have the desired effect. Then I tried Find Next just see if the regex could find anything, but it gave an error as shown in the attached image.
Thanks,
-
@LarLei said in Replace bookmarked lines from one file to bookmarked lines in another file.:
but it gave an error
Well, it’s not actually an error. It is saying there were no instances found of the text you were looking for between the caret and the last character in the file. Suggest confirming the caret (the marker pointing to where new text would be inserted if you typed some) is at the start of the file and/or use the wrap around option. Wrap around allows the whole file to be searched even if the caret is some way through the file content.
Terry
-
@Terry-R
Hi,
Fair point about not being an error.I copied then pasted (regex) exactly from your instruction. Should it have worked? Because it doesn’t look like it.
Thanks,
-
@LarLei said in Replace bookmarked lines from one file to bookmarked lines in another file.:
Should it have worked? Because it doesn’t look like it.
I’ve absolutely no idea. At no point have you actually outlined what it is you need to do, nor the data you are working with. Then there is the small matter of you saying “I’m trying to adapt this to my own use case” which suggests your need isn’t exactly the same as the one in the opening post.
At this point we could go back and forwards over several (pointless) question/answer posts. Instead I would point you to the FAQ section, specifically the post (Template for Search/Replace Questions) that deals with how you should represent your need/request. From there someone on the forum might help.
Bear in mind that often regular expressions (regex) are created for a single need. Unless someone else’s data is EXACTLY the same and the need to alter it also EXACTLY the same there will often be a need to massage the regex to suit a new request.
Terry
-
Hi,
I’m trying to figure out why the regex worked for the OP and not for me. The data and regex are both from the screenshot.
Here is the data in text format (I copied from Notepad++ as also shown from the screenshot):
01 01 <name>NAME-1</name> 02 <name>NAME 1</name> 03 10 <name>NAME-2</name> 04 <name>NAME 2</name> 05 19 <name>NAME-3</name> 06 <name>NAME 3</name>
Here are the regex (copied/pasted directly from the instruction and also as shown on the screenshot:
Find What:(?-s)^\d+\h(\d+\h)([^<]+).+\R[^<]+(.+\R) Replace With:\1\2\3
Before adapting to my own use case, I’d like to gain a better understanding, so I’m replicating the given example/instruction step by step. Then I ran into the issue with the regex.
Thanks,
-
@LarLei said in Replace bookmarked lines from one file to bookmarked lines in another file.:
Then I ran into the issue with the regex.
Well, the data you show above isn’t exactly as per the screen shot. There are additional spaces behind the 2nd number on 1st, 3rd and 5th lines. The data of yours above doesn’t have those additional spaces.
However I think I can see where it goes wrong for you as those spaces I accounted for, but not for a lack of additional spaces. Try adding a
?
behind the first([^<]+)
, so making it([^<]+)?
.As I said, a solution like this is very specific and in order to adjust for slightly different data you must first understand what it does, especially with the original data, not a close copy.
Terry
-
Hello, @larlei, @terry-R and All,
Given the INPUT text :
01 01 <name>NAME-1</name> 02 <name>NAME 1</name> 03 10 <name>NAME-2</name> 04 <name>NAME 2</name> 05 19 <name>NAME-3</name> 06 <name>NAME 3</name>
And the regex S/R :
-
SEARCH
(?-s)^\d+\h(\d+\h)([^<]+).+\R[^<]+(.+\R)
-
REPLACE
\1\2\3
I can affirm, like you did @larlei, that this search regex does not find any match at all, so cannot allow any replacement !
Of course, I could explain why this regex fails, but, to my mind, it’s not important !
Indeed, the main goal is that you should tell us :
-
Which is your text and/or your files involved, as INPUT
-
Which is the text or files expected, as OUTPUT
Just provide us enough raw text to possibly build a regex S/R allowing the changes
Be aware that, may be, you’ll need scripting to achieve your goals !
Best Regards,
guy038
-
-
Thank you for the explanation! Looking at your screenshot, I now realize each dot represents a space. So I manually recreated that exact text and the regex in your instruction worked as expected.
I also appreciate your suggested regex to work with my data set that has only one space. I might have made some errors as I went through the instruction step by step. So I manually created a data set to resemble what I saw (didn’t realize each dot represents a space) to move forward with the testing.
I now have a decent understanding of this example to try to apply to my own use case. If I run into issue with Search/Replace regex, I’ll use the help template as you suggest.
Thanks again for your time and help!
Thank you for your confirmation and suggestion.
-
Hi, @larlei, @terry-R and All,
However, @larlei, given the same INPUT text :
01 01 <name>NAME-1</name> 02 <name>NAME 1</name> 03 10 <name>NAME-2</name> 04 <name>NAME 2</name> 05 19 <name>NAME-3</name> 06 <name>NAME 3</name>
Then, the modified search regex of @terry-r, below, with an question mark, after the group 2
([^<]+)
:-
SEARCH
(?-s)^\d+\h(\d+\h)([^<]+)?.+\R[^<]+(.+\R)
-
REPLACE
\1\2\3
Does match and, after replacement, you get the expected OUTPUT text :
01 <name>NAME 1</name> 10 <name>NAME 2</name> 19 <name>NAME 3</name>
Let’s go back to the wrong syntax of the regex S/R, without the question mark. Why this regex does not find any match ? Well …
-
From beginning of line, it searches for some digits, followed with one horizontal space character, and this twice ( the part
^\d+\h(\d+\h)
) -
At this point, it tries to find some characters, all different from an opening tag delimiter
<
. But this case is impossible as an opening tag delimiter<
follows, right beforename>
. So the overall search regex fails ! -
Now, adding the exclamation mark, after the group
2
, means that all the group contents are not mandatory. Thus, it can be ignored and the following part.+\R
does match the remaining of the first line, including its like-break (\R
) ! -
And, indeed, I verified that, against our INPUT text, the optional group 2
([^<]+)?
is always EMPTY !
In other words, you can simplify your regex S/R to this syntax :
-
SEARCH
(?-s)^\d+\h(\d+\h).+\R\d+\h(.+\R)
-
REPLACE
\1\2
So, from the INPUT below :
01 01 <name>NAME-1</name> 02 <name>NAME 1</name> 03 10 <name>NAME-2</name> 04 <name>NAME 2</name> 05 19 <name>NAME-3</name> 06 <name>NAME 3</name>
With this new S/R , you would get the same OUTPUT as above, i.e :
01 <name>NAME 1</name> 10 <name>NAME 2</name> 19 <name>NAME 3</name>
BR
guy038
-