Issue
How can I remove all <a href="file://???">
keep this text</a>
but not the other <a></a>
or </a>
using sed or perl?
Is:
<p><a class="a" href="file://any" id="b">keep this text</a>, <a href="http://example.com/abc">example.com/abc</a>, more text</p>
Should be:
<p>keep this text, <a href="http://example.com/abc">example.com/abc</a>, more text</p>
I have regex like this but it is too greedy and removes all </a>
gsed -E -i 's/<a*href="file:[^>]*>(.+?)<\/a>/\1>/g' file.xhtml
Solution
Assumptions:
- OP does not have access to a HTML-centric tool
- remove the
<a href="file:...">
...some_text...</a>
wrappers leaving just...some_text...
- only apply to
file:
entries - input data does not have a line break/feed in the middle of a
file:
entry
Sample data showing multiple file:
entries interspersed with some other (nonsensical) entries:
$ cat sample.html
<p><a href="https:/google.com">some text</a><a href="file://any" >keep this text</a>, <a href="http://example.com/abc">example.com/abc</a>, more text</p><a href="file://anyother" >keep this text,too</a>, last test</p>
One sed
idea to remove the wrappers for all file:
entries:
sed -E 's|<a[^<>]+file:[^>]+>([^<]+)</a>|\1|g' "${infile}"
NOTE: perhaps a bit overkill with some of the [^..]
entries but the key objective is to short circuit sed's
default greedy matching ...
This leaves:
<p><a href="https:/google.com">some text</a>keep this text, <a href="http://example.com/abc">example.com/abc</a>, more text</p>keep this text,too, last test</p>
Answered By - markp-fuso