Help REPLACING content with incremental order (by +1)
-
Hi folks,
In my TXT file I have several lines with the following content:
"HandleId": "1", "HandleId": "13", "HandleId": "5", "HandleId": "2", "HandleId": "11",
I would like to replace the number in a ascendent incremental order by +1, so the lines above would change to:
"HandleId": "1", "HandleId": "2", "HandleId": "3", "HandleId": "4", "HandleId": "5",
It seems easy, but imagine that I have thousands of lines in this context.
Could someone help me with this, please?
best regards,
JR
-
@Joel-Rodrigues said in Help REPLACING content with incremental order (by +1):
Could someone help me with this, please?
Our FAQ section has a whole writeup on mathematical replacement. Go read that, and use one of the methods suggested there.
-
@Joel-Rodrigues Something like this is better handled by a scripting language, but you can be a little cheeky with the Search Results Window and copy its output:
Search "HandleId" (5 hits in 1 file of 1 searched) new 1 (5 hits) Line 1: "HandleId": "1", Line 2: "HandleId": "13", Line 3: "HandleId": "5", Line 4: "HandleId": "2", Line 5: "HandleId": "11",
Then use a RegEx search for “
: "HandleId":.*
” and replace with “",
”:Search "HandleId" (5 hits in 1 file of 1 searched) new 1 (5 hits) Line 1", Line 2", Line 3", Line 4", Line 5",
And again RegEx search for “
^\s+Line\s+
” and replace with “"HandleId": "
”:Search "HandleId" (5 hits in 1 file of 1 searched) new 1 (5 hits) "HandleId": "1", "HandleId": "2", "HandleId": "3", "HandleId": "4", "HandleId": "5",
Then just delete the first two lines.
-
@Joel-Rodrigues said in Help REPLACING content with incremental order (by +1):
Hi folks,
In my TXT file I have several lines with the following content:
"HandleId": "1", "HandleId": "13", "HandleId": "5", "HandleId": "2", "HandleId": "11",
I would like to replace the number in a ascendent incremental order by +1, so the lines above would change to:
"HandleId": "1", "HandleId": "2", "HandleId": "3", "HandleId": "4", "HandleId": "5",
It seems easy, but imagine that I have thousands of lines in this context.
The simplest solution depends a lot on whether you’ve described the problem precisely or omitted some details. If all the lines are the same except for the number, and they are all together (nothing else intervening), you can do it this way:
First, make a rectangular selection that encloses all the numbers and the quotes and commas following them. To do that, click between the number and the preceding quote on the first line; then scroll down using the scroll bar (don’t click in the document or use your keyboard) to the end of the lines you want to change, then hold down Shift and Alt on your keyboard and click on the last line somewhere well to the right of the longest line. You’ll see a selection that covers all the numbers and the closing
",
on each line, plus some extra empty space at the right. It doesn’t matter how much extra space you cover on the right, so long as you get past the end of every line in the selection.Now, type
",
which will leave you with a series of lines ending in"",
.Now, make another rectangular selection starting between the two quote marks at the end of the first line and ending between the two quote marks at the end of the last line. This will have no width (so it will just look like a single, thin vertical line) and will extend across all the lines you are changing.
Next, choose Edit | Column Editor from the main menu. Enter:
Number to insert (selected)
Initial number:1
Increase by:1
Repeat:1
Leading: None
Format: Dec
and click OK.You should now have what you want.
-
Hi folks and thank you so much for your kind attention and support.
Maybe as was highlighted, I should have provided more details.
The lines with “HandleID” content are not in sequence. They are as follows:
"HandleId": "1", (some text here) (some text here) "HandleId": "13", (some text here) (some text here) (some text here) (some text here) "HandleId": "5", "HandleId": "2", (some text here) (some text here) (some text here) (some text here) (some text here) (some text here) (some text here) "HandleId": "11", (some text here)
My intention would to be presented as follows:
"HandleId": "1", (some text here) (some text here) "HandleId": "2", (some text here) (some text here) (some text here) (some text here) "HandleId": "3", "HandleId": "4", (some text here) (some text here) (some text here) (some text here) (some text here) (some text here) (some text here) "HandleId": "5", (some text here)
It´s not necessary to also move the “(some text here)” content together with the HandleID … I just need to change the HandleID number.
Sorry for not providing this detail before.
best regards
JR
-
@Joel-Rodrigues said in Help REPLACING content with incremental order (by +1):
The lines with “HandleID” content are not in sequence. They are as follows:
"HandleId": "1", (some text here) (some text here) "HandleId": "13", (some text here) (some text here) (some text here) (some text here) "HandleId": "5", "HandleId": "2", (some text here) (some text here) (some text here) (some text here) (some text here) (some text here) (some text here) "HandleId": "11", (some text here)
My intention would to be presented as follows:
"HandleId": "1", (some text here) (some text here) "HandleId": "2", (some text here) (some text here) (some text here) (some text here) "HandleId": "3", "HandleId": "4", (some text here) (some text here) (some text here) (some text here) (some text here) (some text here) (some text here) "HandleId": "5", (some text here)
In that case, there are a few options.
The easiest, in my opinion, would be to use the Columns++ plugin. (However, I wrote that plugin, so perhaps I am biased.):
Columns++ isn’t yet in the Plugins Admin for Notepad++; so you’ll need to install it either using the Quick Installer link listed for the latest version, or, if you can’t or don’t want to use that, following the instructions here.
After installing Columns++, open your file in Notepad++.
Select Plugins | Columns++ | Search…, enter:
Find what :
"HandleId":\h*"\d+"
Replace with :"HandleId": "(?=match)"
Search Mode: Regular expressionand click Replace All.
The MultiReplace plugin, which can be installed from Plugins Admin, can do this in a way similar to Columns++, though the details are different.
If you do not want to install a plugin, your problem can be solved with only Notepad++ built-in functions, using a method similar to what I described in my previous post:
-
First insert an increasing number at the beginning of every line in the file. For this, be sure to set Leading: Zeros in the Column Editor dialog so they are all the same width.
-
Then make a rectangular selection of the parts after the added line numbers and sort on that — that will put all the lines beginning with HandleId together.
-
Then replace the numbers on the (now contiguous) HandleId lines the same way I described in my previous post.
-
Then sort the file with nothing selected, which will put the lines back in the original order.
-
Then make a rectangular selection around the line numbers and delete them.
-
-
WOW! Thank you so much!!!
It worked beautifully!btw, I didn´t know your plugin so far. Thank you for sharing it for the public.
best regards
JR