Find Replace <filename000> with <filename001>
-
Hello, hopefully there is an easy way to do this. I have a large snippet of code which references to a file name called:
fire_smoke_multi000
this file is listed in this snippet of code 120 times. Starting from 0 and ending at 119.
Is there an easy way to find every instance
fire_smoke_multi000
and replace it with it’s proper numbering like so:
fire_smoke_multi000
fire_smoke_multi001
fire_smoke_multi002
fire_smoke_multi003
all the way up to… fire_smoke_multi119Essentially it finds the first instance, numbers it 000 then finds the next instance and numbers it 001, then the next instance 002, and so on. All the way up to 119.
Has to be a way to essentially replace the “000” with some sort of XXX value number starting at 000 and going up to 119.
-
- Find/replace
000$
with nothing (regular expressions on). This will remove the final000
from the line. - Move your cursor to the last column of the first line (right after the
i
infire_smoke_multi
). - Open the
Column/Multi-Selection Editor
(with default keybindings, can be accessed withAlt-C
). Use the settings shown in the below picture.
- Find/replace
-
@Mark-Olson The issue I have though is that this is not in a simple list. I have code scattered across the whole page, so I can’t use the column option I don’t think as the code aligns with other code.
-
@reddreddyredd said in Find Replace <filename000> with <filename001>:
I have code scattered across the whole page,
There is a way to do it if ONLY 1 “file_smoke_multi” exists on any one line.
The steps are:
- Number all the lines in the file using Edit, Column Editor, Number to insert increasing by 1. Essentially you would be creating a line number at the start of the line.
- Mark each line which contains the relevant string using Mark with “bookmark line” ticked.
- Cut these lines out and insert into another temporary tab.
- Create new line numbers, these will be the numbers you really want behind the string.
- Use a regular expression to move these to the correct position.
- Copy all the lines back to the original file and re-sort numerically.
- Remove the line numbers.
Terry
PS I think I may have provided this solution some time ago. A bit of searching may find the expanded solution. If I find it I will post the link.
All I could find was this thread, which is much the same, although also has other links to using a pythonscript solution. -
TerryR’s solution should work fine.
PythonScript solution:
from Npp import * ct = 0 def on_match(m): global ct out = '%s%03d' % (m.group(0), ct) ct += 1 return out editor.rereplace('(file_smoke_multi)', on_match)
This converted
file_smoke_multi foo file_smoke_multi bar file_smoke_moola baz file_smoke_munky file_smoke_multi
to
file_smoke_multi000 foo file_smoke_multi001 bar file_smoke_moola baz file_smoke_munky file_smoke_multi002
-
@reddreddyredd ,
If the answers you’ve received so far aren’t sufficient, then the answer is no, unless you write your own script with PythonScript, or make your own plugin. -
@reddreddyredd said in Find Replace <filename000> with <filename001>:
@Mark-Olson The issue I have though is that this is not in a simple list. I have code scattered across the whole page, so I can’t use the column option I don’t think as the code aligns with other code.
Can you determine some single character which does not exist anywhere in your file?
If you can, let’s call that character#
(but use whatever you determine). Also, I’ll assume Windows line ending conventions, but change that as needed. (If you can’t find a single character, a sequence of characters will work just the same, so long as it doesn’t occur anywhere in your file.)First, use Replace to change each occurrence of
\r\n
to#
.
Now you have one single line with#
where the line breaks belong.Next, change each occurrence of
fire_smoke_multi
to\r\nfire_smoke_multi
.
Now you have each occurrence offire_smoke_multi
at the beginning of a line.Use column mode replacement to replace the numbers. Then replace
\r\n
with null, then replace#
with\r\n
. -
@Coises said in Find Replace <filename000> with <filename001>:
\r\n
THIS WORKED! Using your method combined with Terry’s I was able to follow it easily enough and make the changes. Thanks dudes! You rock! Will remember this for the future for sure!
-
Hello, @reddreddyredd, @mark-olson, @terry-r, @lycan-thrope, @coises and All,
@reddreddyredd, I know that you already reached your goal, with all the advices given in the previous posts, but here is an alternate method :
-
Move the caret at the beginning of your first string
fire_smoke_multi000
-
Automatically, place any instance of
fire_smoke_multi000
at the very beginning of lines with the following regex S/R :-
SEARCH
\R(?!fire_smoke_multi000)
-
REPLACE
#
-
-
Do a
120 × 3
rectangular selection of the string000
-
Open the **Column Editor (
ALT + C
) and replace this selection with the appropriate numbering -
Finally, replace any
#
character with a line-break, with the regex S/R :-
SEARCH
#
-
REPLACE
\r\n
OR\n
( for an UNIX file )
-
Best Regards,
guy038
-