Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sed to ignore a pattern as well as match a pattern in same line

Input file

<a href="perl.html">perl</a>     <a href="http://zoidberg.sourceforge.net/out.html">http://zoidberg.sourceforge.net</a>
<a href="zoiduser.html">zoiduser</a>    <a href="perl.html">perl</a>     <a href="http://zoidberg.sourceforge.net/sample.html">http://zoidberg.sourceforge.net</a>

I need to only remove .HTML extension from below URL from above file:

<a href="perl.html">perl</a>
<a href="zoiduser.html">zoiduser</a>

So that the final output should look like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<a href="perl">perl</a>     <a href="http://zoidberg.sourceforge.net/out.html">http://zoidberg.sourceforge.net</a>
<a href="zoiduser">zoiduser</a>    <a href="perl.html">perl</a>     <a href="http://zoidberg.sourceforge.net/sample.html">http://zoidberg.sourceforge.net</a>

This is what I am doing:

sed '/"http\|"www\|"mailto/ ! s|\(.html\)||g' file

But it ignores the line as soon as it matches the first pattern i.e. avoid URLs that start with "http|"www|"mailto.

>Solution :

You can use

sed -E 's/("(http|www|mailto)[^"]*")|\.html/\1/g' file

Details:

  • -E – enables POSIX ERE syntax
  • ("(http|www|mailto)[^"]*") – Group 1 (\1): " and then either http, www, or mailto and then zero or more chars other than " and then a "
  • | – or
  • \.html.html string.

The replacement is Group 1 values.

See the online demo:

#!/bin/bash
s='<a href="perl.html">perl</a>     <a href="http://zoidberg.sourceforge.net/out.html">http://zoidberg.sourceforge.net</a>
<a href="zoiduser.html">zoiduser</a>    <a href="perl.html">perl</a>     <a href="http://zoidberg.sourceforge.net/sample.html">http://zoidberg.sourceforge.net</a>'
sed -E 's/("(http|www|mailto)[^"]*")|\.html/\1/g' <<< "$s"

Output:

<a href="perl">perl</a>     <a href="http://zoidberg.sourceforge.net/out.html">http://zoidberg.sourceforge.net</a>
<a href="zoiduser">zoiduser</a>    <a href="perl">perl</a>     <a href="http://zoidberg.sourceforge.net/sample.html">http://zoidberg.sourceforge.net</a>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading