LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Awk, increment string numbers (https://www.linuxquestions.org/questions/linux-newbie-8/awk-increment-string-numbers-4175580921/)

hurd 05-28-2016 04:13 PM

Awk, increment string numbers
 
Hi,

I have a html book with a lot of notes (~+586). Theses notes are linked to the text like this:
Code:

<a href="#footnote1">
So, notes and text are linked in two ways.

I want to add some notes in the html files.

But, some of them have to be insert, so the numbers will not match the logic flow (1, 555, 3,…).

I thought that Awk could help me. I’m a pure newbie in Awk.

I read a little, and I try something like this, but it does not work:

Code:

awk 'BEGIN { i = 1 }; gsub(/(\<div id\=\"footnote)[0-9]{1,}+(\"\>\n\s*\<p\>\<a class\=\"footnotenth\" name\=\"footnote)[0-9]{1,}+(nth\" href\=\".*?\#footnote)[0-9]{1,}+(anc\"\>)[0-9]{1,}+/,{$i++ "\\1"$i"\\2"$i"\\3"$i"\\4"$i}) ' /tmp/test_copy.html
The output on the terminal is absolutely not what I expect. In fact, only some lines, from the end of the file, are matched, and number in it are incremented.

Thanks

keefaz 05-28-2016 04:42 PM

I don't follow, you mean add notes + links to the notes, then re-number all notes IDs?

jpollard 05-28-2016 04:55 PM

You would have to read the file twice...

First to find the footnote references because the footnote references may already be out of order..., Then internally generate the replacement numbers, process the file again to replace them. Then they would be in order.

Part of the problem is the list of footnotes - they SHOULD be in incremental order - but sometimes are referenced in multiple orders (as in footnote1, footnote4, are referenced first, then footnote2, 3,4,5, then footnote 1 referenced again.

You would do better to make an appendix, then reference the appendix for your notes. Besides being easier, the appendix can be in a separate file.


All times are GMT -5. The time now is 03:12 AM.