LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 01-04-2020, 03:00 PM   #1
HolAmigo
LQ Newbie
 
Registered: Jan 2020
Posts: 4

Rep: Reputation: Disabled
Convert specific word to HTML hyperlink using sed command


Hello,
I need to convert a word to HTML hyper text link .
Each occurrence of the word
Quote:
page NNN
will be converted to
Code:
<a href="#pNNN">
where NNN is the page number.

So:
Quote:
See page 107
Would become:

Code:
See <a href="#p107">page 107</a>
Thanks

Last edited by HolAmigo; 01-04-2020 at 03:01 PM.
 
Old 01-04-2020, 03:07 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Ok. What have you tried, based on your other earlier question? One tip is that the separator for sed does not have to be a slash / and it can be anything else instead:

Code:
sed -e 's|old|new|'

sed -e '\|FOO| s|old|new|'
However, before you go too deeply into this you should probably step back and reexamine the problem. When working with HTML, you will need a proper HTML parser. Or if you are lucky enough to have well-formed XML, then you can use an XML parser.
 
Old 01-04-2020, 08:51 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Code:
sed -r '/page/s,page ([[:digit:]]+),<a href="#p\1">&</a>,'
As Turbocapitalist said, parsing HTML with sed might be problematic.
 
Old 01-05-2020, 01:19 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I see potential corner cases all over the place - like whitespace count f'instance ...
We all know it can work, but any regex has to be absolutely specific - and that means the OP has to know the data precisely. And be able to enunciate it. Precisely.
 
1 members found this post helpful.
Old 01-05-2020, 04:20 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
One more time, an XML editing utility might be more appropriate.
I have a little experience with xmllint, which I think is too limited for this task, and less experience with xmlstarlet, which I think might be appropriate here.
 
1 members found this post helpful.
  


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
regex for phrase like'word-word-word' Zero4 Linux - General 9 07-06-2019 06:36 AM
Word Processing Alternative(s) to Latex, Word, Libreoffice and (x)html ? Xeratul Linux - Software 3 02-20-2017 04:40 PM
How to convert specific word into specific colors in console shell? arfal Linux - Server 3 05-10-2012 12:40 AM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 06:35 AM
XSL need help translating XML to html hyperlink qwijibow Programming 1 03-09-2005 04:36 PM

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

All times are GMT -5. The time now is 03:37 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