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