How do I renumber the numbers of the following superscript in a text document? :-
Hfghh<sup>[1]</sup>. Hfgghc<sup>[1]</sup>. Fhvfh<sup>[2]</sup>. Dfyhh<sup>[3]</sup>. Xfghg<sup>[4]</sup>. Rfchh<sup>[5]</sup>. Ffhvy<sup>[6]</sup>. Xfhhb<sup>[7]</sup>. Tfhfh<sup>[8]</sup>. Igxfg<sup>[9]</sup>. Atgvvh<sup>[10]</sup>. Zfthb<sup>[11]</sup>. Phfdb<sup>[12]</sup>.
The output should be as follows:-
Hfghh<sup>[1]</sup>. Hfgghc<sup>[2]</sup>. Fhvfh<sup>[3]</sup>. Dfyhh<sup>[4]</sup>. Xfghg<sup>[5]</sup>. Rfchh<sup>[6]</sup>. Ffhvy<sup>[7]</sup>. Xfhhb<sup>[8]</sup>. Tfhfh<sup>[9]</sup>. Igxfg<sup>[10]</sup>. Atgvvh<sup>[11]</sup>. Zfthb<sup>[12]</sup>. Phfdb<sup>[13]</sup>.
That is, the numbering should start from "1" and incrementally increase by "1", without any repetition of numbers. I have been doing it one by one but that is cumbersome since there are more than a hundred of them.
I understand how to use Notepad++ but that too involves many steps to get the desired output and I am looking for something less cumbersome. The method I used with the help of Notepad++ was as follows:
<sup>\[\d+\]<\/sup>
in the "Find" field and <sup>[]</sup>
in the "Replace" field and hit "Replace All" which removes all the numbers.<sup>\[\]<\/sup>
in the "Find" field and <sup>[\r\n]</sup>
in the "Replace" field and hit "Replace All" (still with the regular expression mode selected) to put the ]</sup>
on the next line.]</sup>
and keep pressing Alt+Shift+↓ till you reach the last ]</sup>
.<sup>\[\R(\d+)
in the "Find" field and <sup>[$1
in the "Replace" field and hit, "Replace All" which puts what was on the next line as the <sup>[
on the same line as the <sup>[
.<sup>\[(\d+)\s*\]
in the "Find" field and <sup>[$1]
in the "Replace" field and hit, "Replace All" to get rid of the spaces after the numbers.That gives me (or you) the desired output (superscript numbers starting with "1" and incrementally increased by "1", without any repetition of numbers), but it is cumbersome and I want a shorter, less cumbersome process to do the same.
The answer posted at vim, is it possible to add increasing numbers next to words each time the words are repeated in a text? seems good but I don't understand the script nor how to use it.
Preface:
In PowerShell (Core) 7, use the regex-based -replace
operator with a script block to dynamically determine the substitution expression:
$i = 0
@'
Hfghh<sup>[1]</sup>. Hfgghc<sup>[1]</sup>. Fhvfh<sup>[2]</sup>. Dfyhh<sup>[3]</sup>. Xfghg<sup>[4]</sup>. Rfchh<sup>[5]</sup>. Ffhvy<sup>[6]</sup>. Xfhhb<sup>[7]</sup>. Tfhfh<sup>[8]</sup>. Igxfg<sup>[9]</sup>. Atgvvh<sup>[10]</sup>. Zfthb<sup>[11]</sup>. Phfdb<sup>[12]</sup>.
'@ -replace '(?<=<sup>\[)\d+(?=\]</sup>)', { (++$i) }
For an explanation of the regex and the option to experiment with it, see this regex101.com page.
To provide a file('s content) as input, use (Get-Content -Raw yourfile.txt) -replace ...
; pipe to Set-Content
to save the result to the same or a different file, but note that you may need an -Encoding
argument - potentially both with Get-Content
and Set-Content
.
In Windows PowerShell (the legacy, ships-with-Windows, Windows-only edition of PowerShell whose latest and last version is 5.1), you must use the underlying .NET API directly:
$i = @{ Value = 0 }
[regex]::Replace(
'Hfghh<sup>[1]</sup>. Hfgghc<sup>[1]</sup>. Fhvfh<sup>[2]</sup>. Dfyhh<sup>[3]</sup>. Xfghg<sup>[4]</sup>. Rfchh<sup>[5]</sup>. Ffhvy<sup>[6]</sup>. Xfhhb<sup>[7]</sup>. Tfhfh<sup>[8]</sup>. Igxfg<sup>[9]</sup>. Atgvvh<sup>[10]</sup>. Zfthb<sup>[11]</sup>. Phfdb<sup>[12]</sup>.',
'(?<=<sup>\[)\d+(?=\]</sup>)',
{ (++$i.Value) }
)
'Hfghh<sup>[1]</sup>...'
sample string with (Get-Content -Raw yourfile.txt)
You can run a python script within the PythonScript plugin.
If it is not yet installed, install it via Plugins Admin.
import re
counter = 0
def increment(match):
global counter
counter += 1
return '[' + str(counter) + ']'
editor.rereplace(r'\[\d+\]', increment).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With