LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: Disabled
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

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

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

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


Reply

Tags
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 10:40 AM
[bash/awk] How to increment hour in a loop? wasim_jd Linux - Newbie 4 12-08-2011 11:59 AM
[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 09:51 AM
[Bash] increment number in a string czezz Programming 4 07-01-2009 11:34 AM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 07:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:02 PM.

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