Replace In files based on a condition
-
Using the ‘Find in Files’ option, I did a test with a small html file that included the text
OTHER_2
. Another html file did NOT contain this text.
My regex was
Find What(?s)(.+?)(OTHER_2)(.+?)(.)
Replace With:\1\2\3\4
I should say that the last '.` was because I was considering another option, this regex could be a bit shorter but I was only testing.
I listed the folder and filter as *.html (so it would only interact with 2 files). The one with the text had it’s datetime stamp updated, the other did NOT.Then for the second regex, you could select those ‘updated’ files and drop them onto NPP and use the regex on all open files and then save them all.
I think due to the uncertainty of where the 2 texts would be in relation to each other (1 before 2 or 2 before 1) means it makes it complicated to cater for with 1 regex. Together with the lookahead bug I think it’s likely to come unstuck. Maybe 1 regex could do it, but as @PeterJones said “willing to jump through the wonderful hoops that regex”.
Sometimes although regex provides lots of power at our fingertips, maybe sometimes simple (and possibly more steps) means the OP can more easily understand it, and more importantly has the ability to ‘update’ it over time.
Terry
-
ah, I see my approach is slightly different.
- Start a new npp without any files open
- Using also find in files dialog and search for the condition
- right-click in find result window and click open all
- Using find and replace with option Replace All in All Opened Documents
- Done - I hope.
-
Hey thanks @Eko-palypse
I never realised that option existed (yeah I know it was in front on me ALL this time!). I must admit I was somewhat concerned that I was having to ‘touch’ files, although that was tempered by the fact I was ONLY ‘touching’ those that would be later edited anyways.There was an OP a while back that when presented with something like this was adamant they did NOT want the files’ datetime stamp altered unduly.
Terry
-
What?? They want to change files without changing its timestamp ?
Hmm … doesn’t sound healthy … I don’t say they want do bad stuff but
resetting timestamp back to what it was … :-) -
If you are willing to run the search/replace multiple times, if you have files like
<LockingStrategy val="I"/> <SomethingElse/> <LockingStrategy val="I"/> <SomethingElse/> <TaskType val="O"/> <LockingStrategy val="I"/> <SomethingElse/> <LockingStrategy val="I"/> <SomethingElse/>
The the following search+replace (Find in Files) will get all the matches before the condition, and at least one of the matches after the condition. However, if there is more than one after the condition, you will have to run the replace in files more than once.
With a bit more tweaking, I am sure it could be made to work all in one go, but I haven’t been able to figure it out yet.
- Find =
(?s)(<LockingStrategy val="I"/>(?=.*TaskType val="O"/>)|(?=TaskType val="O"/>).*\K<LockingStrategy val="I"/>)
or(?s)(<LockingStrategy val="I"/>(?=.*TaskType val="O"/>)|(?=TaskType val="O"/>).*?\K<LockingStrategy val="I"/>)
- the first will find the LAST match after the condition as the post-condition to be replaced
- the second will find the FIRST match after the condition as the post-condition to be replaced
- the match-before-condition will find all of them.
- Replace =
<LockingStrategy val="O"/>
- Search Mode = Regular Expression
I technically only tried this in one file at a time, but in theory, everything that works in one file will work the same in multiple files. I did verify it worked if there were no matches after the condition, and it “works” (replaces one at a time) if all the matches are after the condition.
I’ve got another idea, but not sure if I’ll have time to test it tonight (and will have forgotten it by tomorrow), so I’ll post for now.
- Find =
-
Yeah, the second idea isn’t going to work as I had it.
What I am looking for is a way (similar to \K) to reset the beginning of the match back to the beginning of the
TaskType val="O"/>
after the replacement has occurred, so that it’s searching from that same spot again to find the next match. But I don’t know how to do that.For now, the best I have is to run the replace-in-files multiple times until they’re all gone. Not ideal, I know, especially if there might be thousands of matches after the conditional. Which is why I’m still an advocate for scripting this, not regexing it.
-
Hello, @graham-white and All,
To be as accurate as possible, I just think that the best would be to get one or some samples of your
XML
files, first ;-))So, if you don’t mind, and if your data are not personal nor confidential, simply attach some examples to your mail to :
See you later,
Best Regards,
guy038
-
@Eko-palypse I am unsure what you mean in step 2 by “Using also” can you please elaborate
Regards
-
Don’t be confused by my new name - verification for my account via google isn’t possible at the moment
so I had to register an account at github. phew …What I meant is,
You start a npp session without any documents loaded or use an
existing npp session and close all documents.Then use the standard find in files dialog and search for the condition
As a result of this search you should get a new find result windows.Within the new result window you can right-click and use Open All.
Now you should have all files open which do have the condition in it.Last step would be using the normal find/replace dialog and define
your find and replace arguments and use the button Replace All in All Opened DocumentsThis should result in what you want to have.
-
did the google login symbol disappear as an option when you tried to login again, or was there a problem after clicking on the google g icon ?
if the google login just disappeared as an option: make sure you are logged in at google.com, logoff your git account at this community, disable any adblocker or js-blocker and reload the community login page.
-
no, the button is still there.
When I tried to log in, I was allowed to
enter the name and the password, after that I was
asked what my cell phone number was, since I could not become verified.
As I couldn’t provide the first possibility nor the second
for my account verification, because initially not specified at all,
I get now with every attempt the hint that google could not verify me.I even get this as confirmation in my gmail inbox,
where I can log in without any problems.
As a hint I get that someone tried to log on from another computer,
right around the time I’m tried to log on here.Strange…
-
if you are able to open for example gmail in your browser, go to https://myaccount.google.com/security and change or set your verification methods.
then retry login and verification.As a hint I get that someone tried to log on from another computer,
right around the time I’m tried to log on here.you will get that, if you entered anything wrong at the verification.
unfortunately google does not provide you with an ip address and/or physical location of the attempted login (like apple does 😉 ), so it’s recommended to change your password as well, as long as you aren’t sure that it was you. -
oh google is giving me my ip and even my city - very accurate but within
the security verification I do get the same issue - I should provide my
mobile or my landline telephone number or provide my postal address.But I do not like to provide this info - google knows already too much about me.
I guess I stay with github account.Would you mind giving me my second reputation point, please … pleeeease?
-
@Meta-Chuh
THANK YOU :-D -
Hi, @graham-white, and All,
I did receive your
Prg_99.xml
file and I’ve begun to study it. It seems very well structured and, seemingly, the tag<TaskType val="."/>
is always followed, further on, with the<LockingStrategy val="."/>
tag, and are both located inside a<Task MainProgram="N">..... </Task>
blockYou said, in your mail, that if you find a
<TaskType val="O"/>
tag, followed, in a same<Task ....</Task>
block, with a<LockingStrategy val="M"/>
tag , then you need to change it into<LockingStrategy val="O"/>
So, a possible regex S/R would be :
SEARCH
(?s-i)^\h+<TaskType val="O"/>.+?<LockingStrategy val="\KM(?="/>)
REPLACE
O
Remarks :
-
Of course, don’t forget to select the
Regular expression
search mode -
You may test it, finding all the
M
letters which must be changed toO
, in the<LockingStrategy.../>
tag -
When replacing, just click on the
Replace All
button, exclusively. Do not use theReplace
button !
Notes :
-
The
(?s)
in-line modifier means that any dot will match any character ( standard or EOL ones ) -
The
(?-i)
in-line modifier means that the search is processed in a non-insensitive way -
Then, the
^\h*
looks for any non-null range of horizontal blank characters -
The parts
<TaskType val="O"/>
and<LockingStrategy val="
searches for these literal expressions -
And the
.+?
represents the multi-lines area of characters, between the<TaskType.../>
tag and theLockingStrategy.../>
tag -
Now, the
\K
syntax cancel the previous search and start a new search process -
The
M
part obviously matches the letter to be changed -
But, only if followed with the string
"/>
, due to the positive look-ahead structure,(?="/>)
Hope this helps !
Best Regards,
guy038
P.S. :
My regex will find any
M
letter, in any<LockingStrategy.../>
tag, which follows a<TaskType val="O"/>
tagSo,
11
occurrences, in lines2743
,10775
,13868
,16334
,16705
,17232
,17951
,18446
,20815
,21407
and21791
The layout of these tags is ;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 15: <Task MainProgram="N"> Line 41: <TaskType val="O"/> Line 524: <LockingStrategy val="O"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 1980: <Task MainProgram="N"> Line 1990: <TaskType val="O"/> Line 2743: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 10667: <Task MainProgram="N"> Line 10677: <TaskType val="O"/> Line 10775: <LockingStrategy val="M"/> Line 11257: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 11258: <Task MainProgram="N"> Line 11268: <TaskType val="B"/> Line 11306: <LockingStrategy val="M"/> Line 11516: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 11517: <Task MainProgram="N"> Line 11527: <TaskType val="B"/> Line 11581: <LockingStrategy val="M"/> Line 11856: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 11857: <Task MainProgram="N"> Line 11867: <TaskType val="B"/> Line 11921: <LockingStrategy val="M"/> Line 12246: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 12247: <Task MainProgram="N"> Line 12257: <TaskType val="B"/> Line 12297: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 12509: <Task MainProgram="N"> Line 12519: <TaskType val="B"/> Line 12769: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 13272: <Task MainProgram="N"> Line 13282: <TaskType val="B"/> Line 13328: <LockingStrategy val="M"/> Line 13581: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 13582: <Task MainProgram="N"> Line 13592: <TaskType val="B"/> Line 13621: <LockingStrategy val="M"/> Line 13771: </Task> Line 13772: </Task> Line 13773: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 13774: <Task MainProgram="N"> Line 13784: <TaskType val="O"/> Line 13868: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 16072: <Task MainProgram="N"> Line 16082: <TaskType val="B"/> Line 16132: <LockingStrategy val="M"/> Line 16284: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 16285: <Task MainProgram="N"> Line 16295: <TaskType val="O"/> Line 16334: <LockingStrategy val="M"/> Line 16654: </Task> Line 16655: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 16656: <Task MainProgram="N"> Line 16666: <TaskType val="O"/> Line 16705: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 17183: <Task MainProgram="N"> Line 17193: <TaskType val="O"/> Line 17232: <LockingStrategy val="M"/> Line 17694: </Task> Line 17695: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 17696: <Task MainProgram="N"> Line 17706: <TaskType val="B"/> Line 17744: <LockingStrategy val="M"/> Line 17894: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 17895: <Task MainProgram="N"> Line 17905: <TaskType val="O"/> Line 17951: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 18396: <Task MainProgram="N"> Line 18406: <TaskType val="O"/> Line 18446: <LockingStrategy val="M"/> Line 18567: </Task> Line 18568: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 18569: <Task MainProgram="N"> Line 18579: <TaskType val="B"/> Line 18620: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 19369: <Task MainProgram="N"> Line 19379: <TaskType val="B"/> Line 19433: <LockingStrategy val="M"/> Line 20278: </Task> Line 20279: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 20280: <Task MainProgram="N"> Line 20290: <TaskType val="B"/> Line 20328: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 20484: <Task MainProgram="N"> Line 20494: <TaskType val="B"/> Line 20532: <LockingStrategy val="M"/> Line 20698: </Task> Line 20699: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 20700: <Task MainProgram="N"> Line 20710: <TaskType val="O"/> Line 20815: <LockingStrategy val="M"/> Line 21338: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 21339: <Task MainProgram="N"> Line 21349: <TaskType val="O"/> Line 21407: <LockingStrategy val="M"/> Line 21725: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 21726: <Task MainProgram="N"> Line 21736: <TaskType val="O"/> Line 21791: <LockingStrategy val="M"/> Line 22172: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 22173: <Task MainProgram="N"> Line 22183: <TaskType val="B"/> Line 22221: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 22508: <Task MainProgram="N"> Line 22518: <TaskType val="B"/> Line 22556: <LockingStrategy val="M"/> Line 22717: </Task> Line 22718: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 22719: <Task MainProgram="N"> Line 22729: <TaskType val="B"/> Line 22768: <LockingStrategy val="M"/> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 22965: <Task MainProgram="N"> Line 22975: <TaskType val="B"/> Line 23021: <LockingStrategy val="M"/> Line 23289: </Task> Line 23290: </Task> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 23291: </Task> Line 23292: </Task>
-
-
@guy038 Awesome thanks so much.
-
Hello @guy038 and All,
In my project there 1000’s xml files in which content needs to change from “Text” to “Requirement” based on certain criteria.Criteria:
"Where Title field is starting from “RQ …” only those Type needs to changed from “text” to “requirement”E.g.
<field id=“title”>RQ The <system> hall comply with “highly recommended” and “recommended” m…</field>
<field id=“type”>text</field> -
ello, @akshay-kapoor and All,
If the
type
attribute of thefield
tag just follows thetitle
attribute, on a next line, the text to search is the literaltext
string and the text to replace the literalrequirement
string, then the following regex S/R should work :SEARCH
(?-si)<field id="title">RQ.+</field>\R<field id="type">\Ktext
REPLACE
Requirement
Use the
Replace All
button, only ( not theReplace
one )Best Regards,
guy038
-
Hi, @akshay-kapoor and All,
Following your e-mail which contained an attached
XML
file, listed below :<?xml version="1.0" encoding="UTF-8"?> <work-item> <field id="_document" type="string">Driveline Control</field> <field id="_space" type="string">Sandbox</field> <field id="author">admin</field> <field id="created">2021-05-03 20:37:58.076 +0530</field> <field id="description" text-type="text/html">The &lt;system&gt; supplier shall comply the assigned ASIL</field> <field id="originalDcsid" text-type="text/html" type="text/html">DCSID1234<br/></field> <field id="priority">25.0</field> <field id="severity">normal</field> <field id="status">open</field> <field id="title">RQ The <system> supplier shall comply with "highly recommended" and "recommended" m...</field> <field id="type">text</field> </work-item>
It’s obvious that my regex could not work ! Indeed, after the
\R
regex notation standing for line-break character(s), it was expecting the literal string<field
. Unfortunately the line<field id="type">text</field>
contains leading space chars !IIt was not explicitly mentioned in your original post, as you just said :
E.g.
<field id=“title”>RQ The <system> hall comply with “highly recommended” and “recommended” m…</field>
<field id=“type”>text</field>
So, use the following correct regex S/R :
SEARCH
(?-si)<field id="title">RQ.+</field>\R\h*<field id="type">\Ktext
REPLACE
Requirement
Use the
Replace All
button, only ( not theReplace
one )I added the
\h*
regex notation which means any range, possibly empty, of horizontal blank chars (Tabulation
,Space
andNo-break space
characters )BR
guy038
-
@guy038 Awesome, its working.
Thank you so much.