I have a file with a number of urls in the form:

http://www.edu-factory.org/wp/about/

I'd like to extract the sitehost of the url and append it to the end of the line:

http://www.edu-factory.org/wp/about/ www.edu-factory.org

Any idea how to do that?

link|improve this question

0% accept rate
feedback

1 Answer

One way to do that:

:s:\(.*//\)\([^/]*\)\(/.*\):\1\2\3 \2:

Explanation:

  • :s: - Using substitution
  • \(.*//\) - Capture the part up to the double slash (group 1)
  • \([^/]*\) - Capture everything that's not a slash (group 2)
  • \(/.*\) - Capture the first single slash and everything after it (group 3)
  • :\1\2\3 \2: - Output groups 1, 2 and 3 followed by a space and group 2 again

Edit:

You could sorten that to:

:s:\(.*//\)\([^/]*\)\(/.*\):& \2:

The ampersand outputs the whole matched string which includes the whole line since the regex begins and ends with .*. You could use \0 instead of the ampersand.

link|improve this answer
And is there an output group that contains the whole line in which there was a match? – sumek Feb 28 '11 at 13:52
@sumek: Please see my edited answer. – Dennis Williamson Feb 28 '11 at 14:14
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.