Replace bookmarked lines from one file to bookmarked lines in another file.
-
@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
-