Erik van de Ven Erik van de Ven - 1 year ago 47
Perl Question

Get all <a> tags which do not contain a rel="nofollow"

I've written a small command to find all files which contain external url's, using

and open them in sublime, so I can do a find and replace on all tags and add a

sublime $(ack -l '<a[^>]+href="http')

But now I would like to make sure those
tags do not already contain a
. Anyone who can help me out?

I just need to get all
tags which contain a
(so I'm pretty sure it's an external url), but probably it's better if I could check it contained a
href="<do not contain>"
, so a href without
. And it may not contain a

Would be a great bonus if it could check on
(so single and double quotes, same for
) But I could run the same command a couple of times, with and without double quotes, so it wouldn't be that much of an issue.

Answer Source

I believe ack uses Perl regex patterns, in which case you should use a negative look-ahead, like this

$ sublime $(ack -l '<a\b(?=[^>]+\bhref="http)(?![^>]+\brel="nofollow")')

But note that ack will check only one line at a time whereas an HTML <a> element may run over several lines

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download