Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-28-2016, 05:13 PM   #1
LQ Newbie
Registered: Jan 2015
Posts: 13

Rep: Reputation: Disabled
Awk, increment string numbers


I have a html book with a lot of notes (~+586). Theses notes are linked to the text like this:
<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:

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.


Last edited by hurd; 05-28-2016 at 05:17 PM. Reason: unintentional sent
Old 05-28-2016, 05:42 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 5,842

Rep: Reputation: 588Reputation: 588Reputation: 588Reputation: 588Reputation: 588Reputation: 588
I don't follow, you mean add notes + links to the notes, then re-number all notes IDs?
Old 05-28-2016, 05:55 PM   #3
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,715

Rep: Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281
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.


awk, awk regex, html, incrementing, loop

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySql Doesn't Reuse Numbers in Auto-Increment dman777 Programming 5 03-31-2013 11:40 AM
[bash/awk] How to increment hour in a loop? wasim_jd Linux - Newbie 4 12-08-2011 12:59 PM
[SOLVED] find the total of numbers that are higher than x in a text file with numbers (using awk??) Mike_V Programming 12 11-24-2010 10:51 AM
[Bash] increment number in a string czezz Programming 4 07-01-2009 12:34 PM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 08:01 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration