Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I renumber the numbers of this superscript?

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:

  1. Open this above mentioned text document in Notepad++ and press Ctrl+H,
  2. Select the regular expression mode and type <sup>\[\d+\]<\/sup> in the "Find" field and <sup>[]</sup> in the "Replace" field and hit "Replace All" which removes all the numbers.
  3. Type <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.
  4. Put the cursor just before the first ]</sup> and keep pressing Alt+Shift+ till you reach the last ]</sup>.
  5. Press Alt+C and in the new pop-up, select the "number to insert" option, then the "Dec (decimal)" option below and type "1" where it asks for the, "Initial number", "1" where it asks for the, "Increase by" and "1" where it asks for the, "Repeat" and hit "OK" which will add the numbers incrementally but with an extra, unwanted space after the numbers 1 to 9.
  6. Type <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>[.
  7. Type <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.


2 Answers

Preface:

  • This answer is a PowerShell-only solution, based on input text provided either directly or via a file; it does not address the Notepad++ angle.

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) }
)
  • Again, to provide input from a file, replace the 'Hfghh<sup>[1]</sup>...' sample string with (Get-Content -Raw yourfile.txt)
like image 168
mklement0 Avatar answered Sep 02 '25 22:09

mklement0


You can run a python script within the PythonScript plugin.

If it is not yet installed, install it via Plugins Admin.


  • Create a script (Plugins >> PythonScript >> New Script)
  • Copy this code and save the file (for example increment.py):
import re

counter = 0
def increment(match):
    global counter
    counter += 1
    return '[' + str(counter) + ']'

editor.rereplace(r'\[\d+\]', increment).
  • Open the file you want to modify
  • Run the script (Plugins >> PythonScript >> Scripts >> increment)
  • Done
like image 24
Toto Avatar answered Sep 02 '25 22:09

Toto