Adding text with variable on several files
-
HI, I’m quite green with python scripting, and I have found a problem which is well above my knowledge of notepad++ and python.
I have several files with the structure:
HEADERparty = {
name = “ALE_section_four”
412354party = {
name = “ALE_section_five”
7655444556party = {
name = “ALE_section_six”
876533454The thing is I want to store the three characters just before _section (ALE in this case) and then copy the following text just after the HEADER,
party = {
name = “s%_section_one”
62194678party = {
name = “s%_section_two”
112093party = {
name = “s%_section_three”
87321345replacing the s% for the stored variable (ALE in this case). so the final result would be something like this:
HEADER
party = {
name = “ALE_section_one”
62194678party = {
name = “ALE_section_two”
112093party = {
name = “ALE_section_three”
87321345party = {
name = “ALE_section_four”
412354party = {
name = “ALE_section_five”
7655444556party = {
name = “ALE_section_six”
876533454This is well beyond my current abilities, so i would accept any help on the issue, thank you very much.
-
I suppose something like this works:
Set search mode to Regular expression
find:
(?-s)^HEADER(\R)\R(?=party = {\Rname = "(...)_section_four")
replace:
HEADER${1}${1}party = {${1}name = "${2}_section_one"${1}62194678${1}${1}party = {${1}name = "${2}_section_two"${1}112093${1}${1}party = {${1}name = "${2}_section_three"${1}87321345
-
@Alan-Kilborn Is not finding it
-
You may need to adjust your double-quote characters in the “find” and “replace” expression I gave.
If you look closely at your screenshot data, you’ll see that yours are “slanted” and I used “straight” in my expressions. They are not interchangeable – welcome to a unicode world.
I did what I did because you pasted data into this site without preserving its format, so I made my “best guess” at what you had.
Or, it could be something else wrong.
But…I’d start there. -
@Vincent-Perez said in Adding text with variable on several files:
“ALE_section_one”
You were right! thank you very much, you have saved me a lot of hours of boring copy paste :-)
-
Hi, @vincent-perez, @alan-kilborn and All,
If I suppose that your text contains, after the
HEADER
line,n
blocks as below ( syntaxxxx
means any text ) :party = { name = "s%_section_xxx" xxxxx
In turn, followed with
m
blocks as below :party = { name = "XXX_section_xxx" xxxxx
And if
m
is greater than0
( i.e. if exists, at least, one block with a linename = "XXX_section_xxx"
where the stringXXX
must replace the strings%
)An other solution could be :
SEARCH
(?s-i)s%(?=.*?^name\x20=\x20"(\w+)_section)
REPLACE
\1
Notes :
-
Unlike the @alan-kilborn’s solution, this S/R does not care about numbers, under the
name = •••
line and about text after_section_
, too. It just try to match any%s
string and stores the firstXXX
string found, before_section
, for use in replacement -
Note that if NO block with a line
name = "XXX_section_xxx"
can be found in a whole section ( casem = 0
), all stringss%
, in linesname = "s%_section_xxx"
of this section, would be changed asXXX
, whereXXX
comes from the firstname = "XXX_section_xxx"
line of the next section
Best Regards,
guy038
-
-
@guy038 I have to add 9 lines of matter (text), all on new lines, one after the other in multiple files after this code/text: <button class=“button-top”><i class=“fa fa-chevron-up”></i></button> - how do I do it?
-
@Ramanand-Jhingade said in Adding text with variable on several files:
all on new lines, one after the other
Using
\r\n
in the REPLACE field indicates the CRLF newline. (If you’re editing a file with a linux LF newline, just use\n
).So if you wanted the replacement to be
blah second line third line
then REPLACE would be
blah\r\nsecond line\r\nthird line
-
@Ramanand-Jhingade The only solution I can think of now is to use the “Extended” mode of search, search for <button class=“button-top”><i class=“fa fa-chevron-up”></i></button> and replace it with <button class=“button-top”><i class=“fa fa-chevron-up”></i></button>\nxxxxxxxxx where xxxxxxxxx is the new line for each line, 9 times but is there a way to do it all together?
Apart from what @PeterJones typed above? -
Sorry, yes, my reply was assuming Regular Expression mode, because that’s the mode that was being used in the conversation between @Vincent-Perez and @guy038 – I didn’t realize when I replied that you weren’t the original poster.
With 9 lines, you will just have to have nine instances of
\r\nxxxxxxx
in the replacement.To avoid some duplication of data, I suggest you use the $0 replacement (see docs), which will use the value matched from the FIND, rather than manually retyping that initial data that you want to keep:
- FIND =
<button class="button-top"><i class="fa fa-chevron-up"></i></button>
- REPLACE =
$0\r\nline 1\r\nline2\r\nline3\r\nline 4\r\nline 5\r\nline six\r\nline 7\r\nline eight\r\nline 9!
- SEARCH MODE = regular expression
(I continue to use
\r\n
in my examples, because chances are, since you’re editing files on using a windows application, the newlines are probably windows-style CRLF newlines) - FIND =
-
@PeterJones Wonderful but do I need that exclamation mark at the end (in the REPLACE field)?
-
@Ramanand-Jhingade said in Adding text with variable on several files:
but do I need that exclamation mark at the end (in the REPLACE field)?
Only if you want it there.
Honestly, some experimentation will quickly provide the answer to that, rather than asking here and waiting the time it takes for a response. -
@Alan-Kilborn Yes, you’re right. Thanks for the help @Alan-Kilborn @PeterJones and @guy038