ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am learning SED and have happened upon a substitution that I simply cannot figure out. I have a page of HTML with anchor tags and links that contain spaces. I must replace all spaces only within the anchor tags. That is, I need a global substitution that is limited within a string. For example:
originals:
<a href="#So is there more than one Library Committee">So is there more than one Library Committee?</a><br />
<a href="#Where are the Libraries?">Where are the Libraries?</a><br />
substitutions needed:
<a href="#So_is_there_more_than_one_Library_Committee">So is there more than one Library Committee?</a><br />
<a href="#Where_are_the_Libraries?">Where are the Libraries?</a><br />
original:
<a name="So is there more than one Library Committee" id="So is there more than one Library Committee"></a>
<h4 align="center">So is there more than one Library Committee?</h4>
substitution needed:
<a name="So_is_there_more_than_one_Library_Committee" id="So is there more than one Library Committee"></a>
<h4 align="center">So is there more than one Library Committee?</h4>
This uses GNU sed -r so the '( ) |' characters don't need escaping, which makes the code more readable.
It uses a loop ':a.......ta' which replaces one space on each loop cycle.
"[^"]* limits 'greedy matching' so that replacements are only made within the double quotes.
Thank you so much!!! You are a hero for taking the time to invest in someone else's learning. It works perfectly!
As I studied the regex I was initially confused by "[^"]* limits 'greedy matching' because I was incorrectly assuming that as we looped we would be replacing the first space encountered until we eventually exhausted all matches. Now I see that we are actually replacing the last space encountered, then working backwards until all matches are exhausted.
I re-tested again, but this time I made the * reluctant:
sed -r ':a s/(<a (href|name)="[^"]*?) /\1_/;ta'
It still works perfectly, but I am assuming the regex engine now replaces the first space matched, continuing onward to the last match. Is my assumption correct?
Greedy matching can be limited in a Perl regex with *? but not in GNU sed.
Removing the loop ':a......ta' shows that in spite of using *? instead of * it still replaces the last space first.
sed -r 's/(<a (href|name)="[^"]*?) /\1_/'
*? seems to be the same as * in GNU sed -r
For example
echo 'abcXdefXg' | sed -r 's/a.*?X//'
g
The output is not 'defXg' so matching has not been limited to the first 'X'.
# Your method works for me
echo '<a name="Education, Careers, and Outreach">' |
sed -r ':a s/(<a (href|name)="[^"]*)( |\,)+/\1_/;ta'
<a name="Education__Careers__and_Outreach">
# I get your output if the loop is removed
echo '<a name="Education, Careers, and Outreach">' |
sed -r 's/(<a (href|name)="[^"]*)( |\,)+/\1_/'
<a name="Education, Careers, and_Outreach">
# The \ and + in ( |\,)+ aren't necessary.
# [ ,] can be used instead of ( |\,)+
sed -r ':a s/(<a (href|name)="[^"]*)[ ,]/\1_/;ta'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.