[Gllug] sed question: hyperlinking URLs

Joel Bernstein joel at fysh.org
Wed Jan 24 16:09:20 UTC 2007


On Wed, Jan 24, 2007 at 04:06:53PM +0000, Joel Bernstein wrote:
> On Wed, Jan 24, 2007 at 03:47:01PM +0000, J F wrote:
> > I'm trying to write a sed command to pick out URLs in a file and turn them into an HTML hyperlink. I've come with this thus far:
> 
> Does it *have* to be sed? You can adapt the perl regex below to sed
> format but it's nice to have a slightly terser format with character
> class escapes. 
> 
> Does this do what you need?
> jbernstein at bandicoot 16:03 ~ $ cat /tmp/foo
> Visit www.google.com.
> foo www.foo.com
> jbernstein at bandicoot 16:03 ~ $ perl -ple
> 's{(www\.[\w\d]+\.[\w\.]*\w)}{<a href="$1">$1</a>}g' /tmp/foo
> Visit <a href="www.google.com">www.google.com</a>.
> foo <a href="www.foo.com">www.foo.com</a>
> 
> I can't see anything wrong with that for what you requested. 
> I dare say somebody will manage to golf the regex down a little - the
> key feature is the final \w (word character) to ensure you don't match
> the trailing "."

Oh, I forgot the http://.../ bit. You can add that yourself. 

Background: use s{}{} rather than s/// so that you don't need to excape
the http://.../ bit

/joel
-------------- next part --------------
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list