LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Convert specific word to HTML hyperlink using sed command (https://www.linuxquestions.org/questions/linux-newbie-8/convert-specific-word-to-html-hyperlink-using-sed-command-4175667094/)

HolAmigo 01-04-2020 03:00 PM

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

Turbocapitalist 01-04-2020 03:07 PM

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.

crts 01-04-2020 08:51 PM

Code:

sed -r '/page/s,page ([[:digit:]]+),<a href="#p\1">&</a>,'
As Turbocapitalist said, parsing HTML with sed might be problematic.

syg00 01-05-2020 01:19 AM

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.

ondoho 01-05-2020 04:20 AM

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.


All times are GMT -5. The time now is 09:33 PM.