LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2011, 08:25 AM   #1
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Rep: Reputation: 0
Awk - substitution with parts of the matched regular expression used.


I remember reading that using sed, you can do this with parentheses:
s/abc(something)def/(something)else/g

I can't find an explanation of how to do something like this with Awk.
Say you have this in an HTML file, where (number) stands for a one or two-digit number:
<sup>(number)</sup>

And you want to change that to this:
<a name="(number)t" href="#(number)b"><sup>(number)</sup></a>

How would this be possible? Would you have to use sed?
 
Old 04-30-2011, 08:29 AM   #2
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
This is what I was going to do, exactly, with the pages of "Practical Common Lisp":

Original:
<sup>(number)</sup>
Modified:
<a name="(number)t" href="#(number)b"><sup>[(number)]</sup></a>

The brackets [] I put there to change the <sup>'d number to the <sup>'d number in brackets. From (number) to [(number)].
 
Old 04-30-2011, 08:32 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
GNU awk provides the gensub function. Example:
Code:
awk '{print gensub(/<sup>([0-9]+)<\/sup>/,"<a name=\"\\1t\" href=\"#\\1b\"><sup>\\1</sup></a>","g",$0)}' file
where the reference to the parenthesized field in the regexp is \\1 in the replacement string. See http://www.gnu.org/software/gawk/man...007d_0029-1200 for details.
 
1 members found this post helpful.
Old 04-30-2011, 08:36 AM   #4
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Most specifically, what I was going to do is this:

There are two occurences of <sup>(number)</sup>

One earlier in the file, one later in the file.
I was going to make these changes to the first one:
From <sup>(number)</sup> to <a name="(number)t" href="#(number)b"><sup>[(number)]</sup></a>
and these changes to the second one:
From <sup>(number)</sup> to <a name="(number)b" href="#(number)t"><sup>[(number)]</sup></a>

to create a clickable link between the two, so you don't have to scroll to the bottom to see the notes and back to where you were again to continue reading where you left off.
But this is so generally useful that I would really like to know how to do this.
 
Old 04-30-2011, 08:38 AM   #5
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Thank you.
 
  


Reply



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
[SOLVED] AWK: compare apache dates without using regular expression smallmeans Programming 13 05-17-2010 02:36 AM
[SOLVED] sed or awk help - need to remove text on each line before a regular expression kmkocot Linux - Newbie 15 10-30-2009 03:20 AM
Regular Expression harkonen Programming 6 07-12-2008 12:06 PM
Help with regular expression Feyd-Rautha Programming 8 04-21-2008 11:18 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:07 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
Open Source Consulting | Domain Registration